Cloudkit gives an error while trying to save a record

I'm trying to just save a record in a private user database, but when I run privateDB.saveRecord (), I get an error

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

This account can log into the cloud control panel so that it is the application developer. What other problems can cause this? Any help would really be appreciated; I have been stuck on it 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() } }) 
+6
source share
6 answers

I found that I can’t connect to CloudKit at all until I switch to iCloud; in my case, I was able to do this in a simulator. Also, you cannot enter iCloud on the simulator if you have two-factor authentication installed.

+16
source

I was getting this error while trying to immediately load the list of records into the database. When I did this one at a time (waiting for a response before loading the next one), the problem disappeared.

+2
source

The same thing happened because I was trying to add many entries from a MutableArray. I had to make recursive calls to saveRecord to fix this problem.

 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]; } } 
+2
source

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

+1
source

For me, I called "save new data" and "retrieve data" and "update retrieved data" with the same method. After deleting "save new data" everything works fine again.

0
source

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 this helps.

0
source

Source: https://habr.com/ru/post/977577/


All Articles