Using iOS Notifications, Cryptography and iCloud to build your own Chat App II

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 II, you need to go back to part I first, indeed you need to look at the notifications series before do that ideally.

Ok, so in part I we covered the overall plan. The cryptographic code and the cloud databases we’re going to be using. In this article I we’re going to make a stab at the UI [Using SwiftUI and Combine] and do the cloudKit code.

Lets start with some SwiftUI. I want to try and keep simple as possible cause I am a neophyte when it comes to SwiftUI and I already blinded you with science with the cyptographic code.

I decided to have two picker views and a single text field as my interface. You select the person you want to be and you select the ID of the person you want to talk to.

Lets populate our new database then with a few names. Open your browser window to the CloudKit Dashboard, choose the data tab and add a few directory records to the public database. I’v gone with biblical theme and added Matthew, Mark, Luke and John to mine.

Image for post
Image for post

Make sure you add them to the Public Database, it might still work if you add them to the private one; but only if your two devices are logged into the same iCloud Account, which one assumes they won’t be.

Ok, before we do the interface we got a little more structure to build out under it. Go back to your project and create a Cloud.swift class, within this we’re going to flesh out our CloudKit methods.

In this code we define references to the public databases and initialise it and then define a method to query our directory. Within it was also define a combine protocol object which we send an update to when we get it, in this case the names we find on the database. Our contentView class in the meantime will be watching out for that pingPublisher and react accordingly.

Ok, some Swift UI Code and a kludge. Yes, sorry a kludge which isn’t a new SwiftUI nomenclature, it is a compromise. I want to keep things simple, so I am going to use a fixed size array, and this rule that says I can only deal with eight names. We’ll revisit in a later article. I include everything here cause it makes it far easier to copy and paste.

At the top we created objects of the methods for local and remote notifications, we don’t need local; but I left it in for future developments. Next we defined objects for RSA and CloudKit code.

Next we defined two PickerViews with a message field in between them. The top one you notice has a reference to the pingPublisher we defined in the Cloud.swift code.

Bon, if you’ve been following all along you should be in a position to run it again. Swipe on the top PickerView and a few seconds later it’ll populate both with the names in your directory. We’re almost done, read on.

Written by

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store