Danny Bravo
Danny Bravo

Reputation: 4658

Cannot share CloudKit CKShare record

I'm trying to implement cloud kit sharing in my application, however, whenever I try to share an item using a UICloudSharingController I'm getting a consistent error:

I am presented with the initial share popover for adding people, and then when I select one of the options on how I'd like to send the invitation (i.e: by mail), the UICloudSharingControllerDelegate returns calling:

func cloudSharingController(_ csc: UICloudSharingController, failedToSaveShareWithError error: Error)

And throws the error:

CKError 0x170245d60: "Invalid Arguments" (12); "An added share is being saved without its rootRecord (CKRecordID: 0x1700343e0; recordName=C9FA0E96-3461-4C9E-AB99-3B342A37A07A, zoneID=PrivateDatabase:__defaultOwner_)"

I've already created a custom zone in the private cloud database for the user whose zoneId is "PrivateDatabase". I've created an object and successfully saved it to iCloud and it is linked to the custom zone I previously created. The code I am using to present the UICloudSharingController is as follows:

let object = // A core data representation of a CKRecord //

let share = CKShare(rootRecord: object.record) //record is a CKRecord that is stored with the core data object 
share[CKShareTitleKey] = object.name as? CKRecordValue
share[CKShareThumbnailImageDataKey] = UIImagePNGRepresentation(object.categoryKey.icon()) as? CKRecordValue
share[CKShareTypeKey] = "reverse.domain" as CKRecordValue
share.publicPermission = .readOnly

let sharingController = UICloudSharingController(share: share, container: self.container)
sharingController.delegate = self
sharingController.availablePermissions = [.allowPrivate, .allowReadOnly]
sharingController.popoverPresentationController?.sourceView = sourceView
controller.present(sharingController, animated: true, completion: nil)

What am I missing here?

Upvotes: 0

Views: 1155

Answers (2)

LukeSideWalker
LukeSideWalker

Reputation: 7900

You are using the wrong initializer for an instance of UICloudSharingController. There are two different initializers for two different use cases.

  • If an item is shared the first time (not already shared) you have to create a new instance of CKShare with a rootRecord (as you did in your code) but then you have to initialize your UICloudSharingController with this Initializer init(preparationHandler:)
  • If an item is already in a sharing process, then you have to fetch the existing share from iCloud and initialize your UICloudSharingController with this Initializer init(share:container:)

So, the error in your case is, that you create a new instance of CKShare but then use the wrong initializer.

more from Apple

Upvotes: 2

Red
Red

Reputation: 1459

did you check in iCloud, whether the share has the correct link to the root record in the correct zone etc., and/or the rootRecord is existing...

Upvotes: 0

Related Questions