Cannot share CloudKit CKShare record

回眸只為那壹抹淺笑 提交于 2021-01-01 10:10:55

问题


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?


回答1:


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




回答2:


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...



来源:https://stackoverflow.com/questions/42148151/cannot-share-cloudkit-ckshare-record

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!