Search code examples
iosswiftxcodecloudkit

CloudKit - How to modify the record created by other user


It was OK in development, but when I distributed my app by TestFlight, I have had this problem.

While I was checking some failures, I have guessed that when a user who didn’t create the record try to modify it, can’t do that. By the way, I can fetch all record values on Public Database. Only modification isn’t performed.

In the picture below, iCloud accounts are written in green areas. I predicted that these have to be same.

image: Metadata - CloudKit Dashboard

Now, users are trying to modify a record by the following code:

func modifyRecord() {

    let publicDatabase = CKContainer.default().publicCloudDatabase

    let predicate = NSPredicate(format: "accountID == %@", argumentArray: [myID!])
    let query = CKQuery(recordType: "Accounts", predicate: predicate)

    publicDatabase.perform(query, inZoneWith: nil, completionHandler: {(records, error) in

        if let error = error {
            print("error1: \(error)")
            return
        }

        for record in records! {
        
            /* ↓ New Value */
            record["currentLocation"] = CLLocation(latitude: 40.689283, longitude: -74.044368)

            publicDatabase.save(record, completionHandler: {(record, error) in

                if let error = error {
                    print("error2: \(error)")
                    return
                }
                print("success!")
            })
        }
    })
}

In development, I created and modified all records by myself, so I was not able to find this problem.

Versions

Xcode 11.6 / Swift 5

Summary

I guessed that it is necessary to create and modify record by same user ( = same iCloud Account ) in this code. Then, could you please tell me how to modify the record created by other user? In the first place, can I do that?

Thanks.


Solution

  • Looks like you have a permissions problem. In your cloudkit dashboard, go to: Schema > Security Roles

    SecurityRoles

    Then under 'Authenticated' (which means a user logged into Icloud)

    Authenticated type

    you'll need to grant 'Write' permissions to the relevant record type. That should fix it!

    Permissions