Ios 使用CKCloud工具包共享私有记录

Ios 使用CKCloud工具包共享私有记录,ios,swift,swift3,cloudkit,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",

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

@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为0x60800224560;recordName=C0ADC819-57F7-4D99-A527-B21590F506AB,zoneID=ShareZone:defaultOwner

我看了这个答案,谁也有同样的问题,但不太知道如何让他们的解决方案发挥作用,因为他们没有提供足够的细节


有人知道我做错了什么吗?

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

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")
}

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

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")
}

这看起来很棒,我将如何实现它-你能修改我的代码吗?需要一些修改,但完成了工作!!谢谢这看起来很棒,我将如何实现它-你能修改我的代码吗?需要一些修改,但完成了工作!!谢谢