6

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()
    }
})
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Diericx
  • 418
  • 8
  • 24

6 Answers6

16

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.

nh32rg
  • 1,462
  • 13
  • 15
  • I can't vote up because I don't have enough points :( But thank you so much this fixed it...I don't get why this would be the problem but thank you so much! – Diericx Nov 02 '14 at 23:31
2

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.

user2002649
  • 710
  • 5
  • 9
2

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];

    }
}
pbeaumier
  • 203
  • 2
  • 7
1

I got this error when I tried to create a custom CKRecordZone and save a new CKRecord simultaneously. I fixed the problem by putting the CKRecord save in the completion block of the CKModifyRecordZone method.

powertoold
  • 1,593
  • 13
  • 19
0

For me I was calling "save New data" and "retrieve data" and "update retrieved data" in the same method. Once removed "save new data" all working ok again.

user2511630
  • 3,214
  • 2
  • 17
  • 15
0

I had the same problem with the simulator until I did something like this:

CKContainer.defaultContainer().accountStatusWithCompletionHandler { (accountStat, error) in
        if (accountStat == .Available) {
            print("iCloud is available")
            for wishlist in wl {
                let wlRecord = CKRecord(recordType: "WishList")
                wlRecord.setObject(wishlist.type, forKey: "type")
                wlRecord.setObject(wishlist.term_id, forKey: "term_id")

                publicDB.saveRecord(wlRecord) {
                    record, error in
                    if error != nil {
                        print(error?.localizedDescription)
                    } else {
                        print(record?.objectForKey("type") as! String + " recorded")
                    }
                }
            }
        }
        else {
            print("iCloud is not available")
        }
    }

Hope it helps.

Marcos
  • 461
  • 6
  • 10