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

Image for post
Image for post

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.

Obviously this is part VII, you need to go back to the beginning for it to make any sense, indeed you need to look at the notifications series before do that else your struggle. Most of the parts are just 4 minutes long including code.

I decided to try a major rewrite since part V, and I am going try and flesh out the code here.

Let me do a quick review. We have a messaging app, noob. I used public/private encryption on within it and it worked. But only if you followed a certain sequence when sending messages. I managed to fix the sequence issue, but not in all cases. I went back to the drawing board.

I abandoned encryption and introduced what will be a challenge response solution. Needed to revisit almost all of the classes we defined. Lets start with the first onTap in ContentView.swift

This looks much like the previous one really. Only this time when we search and Update iCloud we file the token and a challenge, the latter of which we’re not yet using.

Above you find the new code for the searchAndUpdate in the cloud.swift file. Very little new here, beyond the fact we’re saving the new fields.

However for the user you want to send a message too, things are very different indeed.

The flow of events goes like this. You select a user and send them a background message requesting to talk. [authRequest]. That looks up the user you want to talk to and rus the the code shown in bold. If they respond positively you get confirmation that then sends an [enableMessaging] response. At the same time you save their credentials to your private database for next time. Once you agreed to talk to someone, the next time the protocol is far shorter, you just need to select them. The code that follows goes into your cloud.swift.

Three methods here. The first looks to see if you already talked. If you have it signals a shortProtocol if not it invokes authRequest2.

The second method asks to talk by posting a background request.

The third is run if you agree to chat, saving your token so that next time the shortProtocol will be invoked. Finally from protocol I just described is played out here in essence. Inside your your AppDelegate.

Note that if the user isn’t logged in/running the app. This will not run and you cannot setup a message dialog with them. Finally there is a minor change to the PostNotification file.

The first few lines of which now look like this.

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