简体   繁体   中英

CloudKit shared record: how to make it editable by the user who received the link

I tried everything, read everywhere, but I'm unable to make a shared record editable by the user who received the link

Everything works fine except the edit performed by the invited user

here's the sharing code (like the WWDC16 video):

    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)

The console always print:

PrivateDB can't be used to access another user's zone

thank you

when you retrive the shared record you need to add the operation to the 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)
    }

but now there is a problem when you try to update the record, you need to know who is the owner, if the owner is the one who shared the record you need to save to the private db, if the owner is another person you need to save to the shared db... so:

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

to know if the record was created by the current user the trick is to compare against CKCurrentUserDefaultName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM