CKShare - 无法修改某些记录错误 - CloudKit

CKShare - Failed to modify some records error - CloudKit

我正在尝试与 CloudKit 中的其他用户共享记录,但我一直收到错误消息。当我点击 table 上的 items/records 之一时,我会看到 UICloudSharingController 并且我可以看到 iMessage 应用程序图标,但是当我点击它时我得到一个错误并且 UICloudSharingController 消失了,有趣的是,即使在错误之后我仍然可以继续使用该应用程序。

这是我的。

代码

var items = [CKRecord]()
var itemName: String?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let item = items[indexPath.row]

    let share = CKShare(rootRecord: item)

    if let itemName = item.object(forKey: "name") as? String {
        self.itemName = item.object(forKey: "name") as? String
        share[CKShareTitleKey] = "Sharing \(itemName)" as CKRecordValue?
    } else {
        share[CKShareTitleKey] = "" as CKRecordValue?
        self.itemName = "item"
    }
    share[CKShareTypeKey] = "bundle.Identifier.Here" as CKRecordValue
    prepareToShare(share: share, record: item)
}


private func prepareToShare(share: CKShare, record: CKRecord){

    let sharingViewController = UICloudSharingController(preparationHandler: {(UICloudSharingController, handler: @escaping (CKShare?, CKContainer?, Error?) -> Void) in

        let modRecordsList = CKModifyRecordsOperation(recordsToSave: [record, share], recordIDsToDelete: nil)

        modRecordsList.modifyRecordsCompletionBlock = {
            (record, recordID, error) in

            handler(share, CKContainer.default(), error)
        }
        CKContainer.default().privateCloudDatabase.add(modRecordsList)
    })

    sharingViewController.delegate = self

    sharingViewController.availablePermissions = [.allowPrivate]
    self.navigationController?.present(sharingViewController, animated:true, completion:nil)
}



// Delegate Methods:
func cloudSharingControllerDidSaveShare(_ csc: UICloudSharingController) {
    print("saved successfully")
}
func cloudSharingController(_ csc: UICloudSharingController, failedToSaveShareWithError error: Error) {
    print("failed to save: \(error.localizedDescription)")// the error is generated in this method
}
func itemThumbnailData(for csc: UICloudSharingController) -> Data? {
    return nil //You can set a hero image in your share sheet. Nil uses the default.
}
func itemTitle(for csc: UICloudSharingController) -> String? {
    return self.itemName
}

错误

Failed to modify some records

这是我看到的...

知道哪里出了问题吗?

编辑:

对了,错误是在cloudSharingController failedToSaveShareWithError方法中产生的

可能是因为您尝试实例化 UICloudSharingController 的方式?我直接从文档中复制了我的内容并且它有效:

let cloudSharingController = UICloudSharingController { [weak self] (controller, completion: @escaping (CKShare?, CKContainer?, Error?) -> Void) in
    guard let `self` = self else {
       return
    }
    self.share(rootRecord: rootRecord, completion: completion)
}

如果这不是问题,那是其中一个或两个记录本身的问题。如果您上传记录而不尝试共享它,是否有效?

编辑添加:

CKShareTypeKey 是什么?我不在我的应用程序中使用它。我还以不同的方式设置了我的系统字段:

share?[CKShare.SystemFieldKey.title] = "Something"

您似乎正试图在不允许的默认区域中共享。来自文档 here

Sharing is only supported in zones with the CKRecordZoneCapabilitySharing capability. The default zone does not support sharing.

所以您应该在您的私人数据库中设置一个自定义区域,并将您的共享和记录保存在那里。

尝试将此添加到您的 info.plist

<key>CKSharingSupported</key>
<true/>