0
votes

I am trying to save a record which I have make in the CloudKit dashboard.

@IBAction func signUpPressed(_ sender: AnyObject) {

    let authInfo = AuthInfo()

    authInfo.email = emailField.text!
    authInfo.firstName = firstField.text!
    authInfo.lastName = lastField.text!
    authInfo.username = usernameField.text!
    authInfo.password = passwordField.text!

    let container = CKContainer.default()
    let privateData = container.privateCloudDatabase

    let record = CKRecord(recordType: "Authentication")
    record.setValue(authInfo.email, forKey: "email")
    record.setValue(authInfo.username, forKey: "username")
    record.setValue(authInfo.firstName, forKey: "firstName")
    record.setValue(authInfo.lastName, forKey: "lastName")
    record.setValue(authInfo.password, forKey: "password")
    privateData.save(record, completionHandler: { record, error in
        if error != nil {
            print(error)

        } else {

        }

And, I'm pretty sure that my code is ok. (CloudKit framework is definitely imported properly and CloudKit is enabled properly with valid developer account and container, no issue there) However I get an error saying that I my have an authorised account when I try and run this code. However, other similar questions seemed to have been solved by making sure that you are logged in to iCloud on the simulator (I am using Xcode 8), however I have checked and double checked that I am logged in to iCloud, so I have no idea what the issue is. How can I get around this. Is it actually something to do with my code?

1
Can you show the actual error message?Yann Bodson
@grimfrog Here it is <CKError 0x60800024e2b0: "Not Authenticated" (1002); "This request requires an authenticated account"; Retry after 3.0 seconds>RufusV

1 Answers

1
votes

Your code looks fine. It has to do with authentication.

Are you using a new iCloud test account or your developer account? If it is a brand new account I would try to log in in a browser via iCloud.com first.

Another option could be to reset the simulator (Reset Content and Settings) and setup iCloud again in Settings > iCloud.