CKShare-未能修改某些记录错误-CloudKit

问题描述 投票:0回答:1

我正在尝试与CloudKit中的其他用户共享一条记录,但是我一直遇到错误。当我点击表上的项目/记录之一时,会显示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)")
}
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
}

错误

无法修改某些记录

这是我看到的...

enter image description here

任何想法可能有什么问题吗?

swift cloudkit ckshare
1个回答
0
投票

看来您不是要设置父级

可能是您尝试实例化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)
}

如果这不是问题,则其中一个或两个记录本身都有问题。如果您在不尝试共享的情况下上传记录,那么行得通吗?

© www.soinside.com 2019 - 2024. All rights reserved.