setting up CloudKit in swift

Back to blogSun Jun 26 2022
setting up CloudKit in swift

This for me had been a little struggle so hopefully, here I will provide you my path and how I overcame some of the many issues correctly setting up CloudKit for my app.

Before you start please make sure you have a paid apple developer account membership, as these capabilities to utilise the CloudKit containers will not be accessible.

So first of all we will need to add the CloudKit capabilities within our Project. So click on your project, under "Signing & Capabilities" we want to hit the little plus button next to All, Debug and Release buttons. Here we can add our "Push Notifications" (with remote notifications). Once that is done we will then need to add our iCloud to our project.

Above your newly added features under iCloud, click on the CloudKit checkbox to active, once this is done you'll need to add a Container. This is fairly simple to implement but for the naming conventions please use the app bundle identifier as follows com.company_name.bundle_identifier.

Now once this is done we can add our code, to allow for our application to link to our system as shown below is just a snippet of code I've used to add an entry in.

 let record = CKRecord(recordType: "UserData", recordID: CKRecord.ID(recordName: id))

  record["email"] = data.email   record["firstName"] = data.firstName   record["lastName"] = data.lastName   let publicDatabase = CKContainer.default().publicCloudDatabase  publicDatabase.save(record) { (_, _) in          UserDefaults.standard.set(record.recordID.recordName, forKey: "userID")   }

Now as shown above is pretty simple code, we are first creating our CKRecord, which will be our data we want to add to our database. We are closing our public database under the default container. Here it should use the default container we set up in the project alternatively we can use a string to identify this with our app bundle identifier which we named our container.

Finally we are then saving our record in the public database.

Now when I ran this piece of code originally, this didn't seem to work as effectively as I imagined it would. The issue I had was that my project couldn't find the ID for my container, providing me with the following error:

Invalid bundle ID for container.

Although this would seem like an easy fix I didn't really take into account the setup for my app developer account to allow myself to test this app.

The following steps seems to have worked for me:

  1. Deselect the container on Xcode

  2. Deselect container on developer.apple.com (and save)

  3. Select container again on website

  4. Regenerate profiles and finally

  5. Select container on Xcode

Oddly these five steps seemed to have fixed my issue. Now before we continue what is the provisioning profile:

Apple’s definition: A provisioning profile is a collection of digital entities that uniquely ties developers and devices to an authorized iPhone Development Team and enables a device to be used for testing.

Oddly enough when developing I hadn't manage to create the connection of my own apple device to my development account I had created. Although I've done a fair bit of testing there hadn't been a formal process to connect this device. Once I had created my profile, under the apple developer account. (Certificates, Identifiers & Profiles > Profiles > plus) I then downloaded the profile, and opened the file in Xcode (closed Xcode down before I opened the file). Once completed I rerun the application on my device and everything worked as expected.