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

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 VI, 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, and you can skip part V [the previous article] if you wish.

Ok, the plan. My problem then is that I want to ensure you don’t get unsolicited noobs from strangers, so I need to protect your token ID that we logged in the public directory.

I did try to encrypt it with your public key, but it didn’t work too well. A simpler solution would be to use a shared secret key. Here is the plan. There is a lot in here, so I’ll try and take it step by step. Let’s start with some POC code.

Firstly we need to add background modes to our app, since the initial notification is going to be a silent one. Do so opening noob project, clicking on the target and then capabilities. You want to add background Modes. Within background Modes you need to tick the box Remote Notifications.

Image for post
Image for post

Now you need to move over to your appDelegate and add this code.

Now head over to the ContentView.swift and add this code.

What it is does is simply pop up an alert telling you your authorising a connection.

What this does do , well it generates the post to yourself in this case that triggers the alert. Finally in the RemoteNotifications.swift file, you need to add/change this code.

The section at the top, tells it to expect a type of notifcation to send and then should you send “background” it’ll constructs a different JSON content to if you don’t. The default type is an “alert”.

The lower piece of code simply includes the type you just send thru. That is the basics in place. If you put everything in place now, you should be able to test it.

I wanted to make sure silent notifications would work, cause we’re going to use them to have this conversation.

  1. You want to send a message to Joe, you check your private DB of nicknames and tokenIDs. You don’t have it.
  2. You send Joe a notification requesting to connect including your tokenID [silently]
  3. Joe get the notification, agrees and sends back his tokenID [again silently]
  4. You get his message, file it and you can now talk freely [using encypted content of course to each other].
  5. Assuming you got an agreement you save Joe token to your private DB.

There isn’t a shared secret in this version, but lets get the conversation going and then we can build one in. Now before I start I suggest you backup your project, cause we need to make some major changes. Better you have something that works to fall back on should it all go wrong.

Keep reading…

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