Member-only story
Using iOS Notifications, Cryptography and iCloud to build your own Chat App IV
I should start this article with a disclaimer, it based on iOS 13, Swift 5 and Xcode 11.x. If you reading this and those numbers look dated, be forewarned.
I should also warn you that notifications and iCloud code, involve Apple’s infrastructure which means you will need an Apple Developers account to use them.
Finally Obviously this is part IV, you need to go back to part I, part II & part II for it to make any sense, indeed you need to look at the notifications series before do that else your struggle.
We’re almost ready for another test. You just need to make a few more changes. Start with ContentView.swift and add this code.
TextField("Message", text: $yourMessageHere, onCommit: {
self.output = self.yourMessageHere
cloud.fetchRecords(name: self.sendingTo!)
}).onReceive(cloudPublisher, perform: { (data) in
print("data ",data)
poster.postNotification(token: data, message: self.yourMessageHere)
})
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
I included the entire gist, you already have the TextField in place, you just need to add these methods to it. And now to the Cloud.swift file.
func fetchRecords(name: String) {
print("fetch ",name)
let predicate = NSPredicate(format: "name = %@", name)
let query = CKQuery(recordType: "mediator", predicate: predicate)
publicDB.perform(query,inZoneWith: CKRecordZone.default().zoneID) { [weak self]…