使用CKCloud工具包分享私人记录

时间:2017-03-07 10:53:24

标签: ios swift swift3 cloudkit

我使用以下代码尝试创建共享私人记录:

@IBAction func testPress(_ sender: Any) {

    let customZone = CKRecordZone(zoneName: "ShareZone")
    let friendRecord = CKRecord(recordType: "Share", zoneID: customZone.zoneID)
    let rootRecord = CKRecord(recordType: "Root", zoneID: customZone.zoneID)

    model.privateDB.delete(withRecordZoneID: customZone.zoneID) { (nil, error) in
        self.model.privateDB.save(customZone) { (nil, error) in
            print("Custom Zone Error = \(error)")
            self.model.privateDB.save(friendRecord, completionHandler: { (nil, error) in
                self.model.privateDB.save(rootRecord, completionHandler: { (nil, error) in
                    self.shareTest(record: friendRecord, root: rootRecord)
                })
            })
        }
    }
}

func shareTest(record:CKRecord, root:CKRecord) {

    record["Name"] = "Test" as CKRecordValue?
    root["Name"] = "Test" as CKRecordValue?

    let ckContainer = CKContainer.default()
    let shareRecord = CKShare(rootRecord: root, share: record.recordID)

    shareRecord[CKShareTitleKey] = "Name" as CKRecordValue?

    let shareController = UICloudSharingController(share: shareRecord, container: ckContainer)
    shareController.delegate = self
    shareController.availablePermissions = [.allowReadOnly]

    self.present(shareController, animated: false)
}

但是,当我按下共享链接的方式时,我将返回错误:

  

CKError 0x6000002535f0:"无效参数" (12); "在没有rootRecord的情况下保存添加的共享(CKRecordID:0x608000224560; recordName = C0ADC819-57F7-4D99-A527-B21590F506AB,zoneID = ShareZone: defaultOwner )"

我看了这个答案Link,他遇到了同样的问题,但不太知道如何让他们的解决方案工作,因为他们没有提供足够的细节。

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

我相信错误消息告诉您需要同时保存共享和根记录。

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

您应该在sharingController的完成处理程序中执行此操作。

sharingController = UICloudSharingController {
                        controller, preparationCompletionHandler in

编辑:您的代码看起来像这样的代码:

@IBAction func testPress(_ sender: Any) {
    let privatedatabase = CKContainer.default().privateCloudDatabase
    let newZoneName = UUID().uuidString
    let recordZone = CKRecordZone(zoneName: "ShareZone")

    privatedatabase.save(recordZone) {  savedRecordZone, error in

        if let error = error {
            print("\(error.localizedDescription).")
        } else if let savedRecordZone = savedRecordZone {
            print("\(error.localizedDescription).")

            let rootRecord = CKRecord(recordType: "RootRecord", zoneID: savedRecordZone.zoneID)
            rootRecord["Name"] = "Test" as CKRecordValue?

            privatedatabase.save(rootRecord) { record, error in
                if let error = error {
                    print("\(error.localizedDescription).")
                } else if let record = record {
                    print("successfully added rootRecord to cloud.")
                    self.shareTest( rootRecord: record)
                }
            }
        }
    }
}

func shareTest(rootRecord:CKRecord) {

    let ckContainer = CKContainer.default()
    let shareRecord = CKShare(rootRecord: rootRecord)

    let sharingController =  UICloudSharingController { controller, preparationCompletionHandler in

        let share = CKShare(rootRecord: record)
        share[CKShareTitleKey] = "Share Title" as CKRecordValue
        share.publicPermission = .none

        let modifyRecordsOperation = CKModifyRecordsOperation( recordsToSave: [rootRecord, share], recordIDsToDelete: nil)
        modifyRecordsOperation.timeoutIntervalForRequest = 10
        modifyRecordsOperation.timeoutIntervalForResource = 10

        modifyRecordsOperation.modifyRecordsCompletionBlock = { records, recordIDs, error in

            if let error = error {
                print(error.localizedDescription)
            }
            if let records = records {
                print("Share and Root records saved successfully")
            }
            preparationCompletionHandler(share, CKContainer(identifier: ckContainerID ), error)
        }

        myCloudDatabase.add(modifyRecordsOperation)
    }
    if let sharingController = sharingController {
        sharingController.availablePermissions = [.allowReadOnly, .allowReadWrite, .allowPrivate]
        sharingController.popoverPresentationController?.sourceView = sender
        sharingController.delegate = self
        self.present(sharingController, animated: true)
    }
}

// MARK: UICloudSharingControllerDelegate
// --------------------------------------
func cloudSharingController(_ csc: UICloudSharingController, failedToSaveShareWithError error: Error) {
    print("Failed to share to cloud: \(error)")
}
func itemTitle(for csc: UICloudSharingController) -> String? {
    return "Please join rootRecord share."
}
func cloudSharingControllerDidStopSharing(_ csc: UICloudSharingController) {
    print("Cloudkit stopped sharing")
}
func cloudSharingControllerDidSaveShare(_ csc: UICloudSharingController) {
    print("Cloudkit started sharing rootRecord")
}