Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudkit gives error when trying to save a record

I am trying to simply save a record to a users private database but when I run privateDB.saveRecord() I get an error saying

Not Authenticated" (9/1002); "CloudKit access was denied by user settings"; Retry after 3.0 seconds>.

This account is able to sign into the cloudkit dashboard so it is a developer for the application. What other issues might cause this? Any help would be really appreciated, I have been stuck on this for so long.

Here is my code:

//variable instantiation
container = CKContainer.defaultContainer()
println(container.containerIdentifier)
publicDB = container.publicCloudDatabase
privateDB = container.privateCloudDatabase

//save record code
let passRecord = CKRecord(recordType: "Password")
passRecord.setValue("testytestPow", forKey: "title")
passRecord.setValue("password02", forKey: "value")
privateDB.saveRecord(passRecord, completionHandler: { (record, error) -> Void in
    if (error != nil) {
        println(error)
    } else {
        NSLog("Saved in cloudkit")
        self.fetchTodos()
    }
})
like image 621
Diericx Avatar asked Nov 01 '14 04:11

Diericx


3 Answers

I found I couldn't connect to CloudKit at all until I upgrade to iCloud drive; in my case I was able to do it in the simulator. Also you can't log in to iCloud on the simulator if you have 2-factor authentication set up, it seems.

like image 100
nh32rg Avatar answered Nov 04 '22 07:11

nh32rg


I was getting this error trying to upload a list of records to the database all at once. When I did it one at a time (waiting for the reply before uploading the next one) the problem went away.

like image 2
user2002649 Avatar answered Nov 04 '22 06:11

user2002649


Had the same thing happening because I was trying to add many records from a MutableArray. I had to do recursive calls to the saveRecord in order to fix this issue.

MyObject *obj = [arrayOfObjects ObjectAtIndex:index];
[self addRecord];

and then in the addRecord method

- (void) addRecord {
    // if all objects added, exit recursive method
    if(index == [arrayOfObjects count]) {
        return;
    }

    MyObject *obj = [arrayOfObjects ObjectAtIndex:index];

    [publicDatabase saveRecord:rec completionHander:^(CKRecord *myRec, NSError *error) {
    if (!error) {
        index++
        [self addRecord];

    }
}
like image 2
pbeaumier Avatar answered Nov 04 '22 07:11

pbeaumier