RufusV
RufusV

Reputation: 407

Saving Record in CloudKit not working [Swift 3]

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?

Upvotes: 0

Views: 1090

Answers (1)

Yann Bodson
Yann Bodson

Reputation: 1654

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.

Upvotes: 1

Related Questions