CloudKit共享记录:如何使接收链接的用户可以编辑它

时间:2017-03-02 12:48:30

标签: swift cloudkit

我尝试了所有内容,随处可读,但我无法通过收到该链接的用户编辑共享记录

除了受邀用户执行的编辑外,一切正常

这里是共享代码(如WWDC16视频):

get2(123)
    .then(data => {
        // success
    })
    .catch(error => {
        // error
    });

控制台始终打印:

    let sharingController = UICloudSharingController { (controller, preparationCompletionHandler) in

           let share = CKShare(rootRecord: record)
           share.publicPermission = .readWrite

           share[CKShareTitleKey] = "Help me to improve data" as CKRecordValue
           share[CKShareTypeKey] = "com.company.AppName" as CKRecordValue

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

           modifyRecordsOperation.modifyRecordsCompletionBlock = { (records, recordIDs, error) in
             if let errorK = error {
                 print(errorK.localizedDescription)
             }
            preparationCompletionHandler(share, CKContainer.default(), error)
           }
     CKContainer.default().privateCloudDatabase.add(modifyRecordsOperation)
                    }

sharingController.availablePermissions = [.allowPublic, .allowPrivate, .allowReadWrite]
sharingController.delegate = self

controller.present(sharingController, animated: true)

谢谢

1 个答案:

答案 0 :(得分:1)

当您检索共享记录时,需要将操作添加到sharedDatabase:

func fetchShare(_ metadata: CKShareMetadata) {
        debugPrint("fetchShare")

        let operation = CKFetchRecordsOperation(recordIDs: [metadata.rootRecordID])
        operation.perRecordCompletionBlock = { record, _, error in
            if let errore = error { debugPrint("Error fetch shared record \(errore.localizedDescription)") }

            if let recordOk = record {
                DispatchQueue.main.async() {

                    self.storage.append(recordOk)
                }
            }
        }
        operation.fetchRecordsCompletionBlock = { (recordsByRecordID, error) in
            if let errore = error { debugPrint("Error fetch shared record \(errore.localizedDescription)") }
        }
        CKContainer.default().sharedCloudDatabase.add(operation)
    }

但是现在当您尝试更新记录时出现问题,您需要知道谁是所有者,如果所有者是共享您需要保存到私有数据库的记录的人,如果所有者是另一个你需要保存到共享数据库的人......所以:

func updateOrSaveRecord(_ record:CKRecord, update:Bool) {

        var db : CKDatabase

        if update == true {

            guard let creatorUserID = record.creatorUserRecordID else { return }

            if record.share != nil && creatorUserID.recordName != CKCurrentUserDefaultName {
                debugPrint("record shared from another user")
                db = CKContainer.default().sharedCloudDatabase
            } else {
                debugPrint("private record")
                db = CKContainer.default().privateCloudDatabase
            }
        } else { db = CKContainer.default().privateCloudDatabase }

        db.save(record) { (savedRecord, error) in

            if let errorTest = error {
                print(errorTest.localizedDescription)
            } else {
                if let recordOK = savedRecord {
                    DispatchQueue.main.async {
                        if update == false {
                            self.storage.append(recordOK)
                        } else {
                            self.dettCont?.updateScreen()
                        }
                        self.listCont?.tableView.reloadData()
                    }
                }
            }
        }
    }

知道当前用户是否创建了记录,诀窍是与CKCurrentUserDefaultName进行比较