Fetching a CKShare (URL)

Supplementing Incomplete Documentation

Unfortuantely, CloudKit sharing isn’t the most well documentated API. This is a bit strange to me, and I’m sure anyone else who uses it, considering its scale in Apple’s first party apps: Numerbs, Pages, Keynote, Notes, etc.

Not too long ago I wrote a series of articles on programmaticly creating a CKShare. It was a really great excerise to help me understand the underlying workflow and architecture of show sharing works…plus I need it for my app Significant Other.

Using the UICloudSharingController doesn’t allow for restricting the number of participants to only 1, thus my need to get closer to the metal.

With any multi-step process there are incidents which could cause the workflow to fail between User A and User B. With the most recent update to Significant Other that I’m currently working on I want to give the owner of the share, in this scenario User A, the ability to resend the share link to User B.

As I started to write the feature I find out pretty quickly that solutions weren’t as straight forward as I would have hoped. When the owner creates the share you need to persist the url, share or root record / shareID.

Due to business rules, Significant Other, doesn’t allow for any of these options. After a bit of reading through the documenation and piecing together a few things the path was a bit clearer.

Neccessity Gives Birth to Solution

Since I wasn’t able to use a local copy of the share from methods listed above I had to find another path. Though I don’t persist the CKShare, url or root record locally for the owner, I do persist the CKRecordZoneID.

3 Step Process

Armed with the CKRecordZoneID you need to

  1. Instantiate the record with the appropriate predicate
  2. Fetch the record from CloudKit
  3. Fetch the share for that record

Gist

There might be a better way, but I like having to grab the latest / greatest version of the share to insure of the most up to date properties and meta-data.