How to create user in CloudKit? [closed]

狂风中的少年 提交于 2019-12-21 21:37:58

问题


In Parse, to make a new user, I used:

var user = PFUser()
user.username = "myUsername"
user.password = "myPassword"
user.email = "email@example.com"

What's the code used in CloudKit to create a new user and use it for login?


回答1:


I had to do something similar recently. I had to create a CKRecord for each user that contained the user's username and password. I added their username to NSUserDefaults to make it easy to retrieve.

Here is the code for signing up:

//Check if users exist
let query = CKQuery(recordType: "MyUsers", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
            pubDB.performQuery(query, inZoneWithID: nil, completionHandler: { (records, error) in

                //error code 11 is no objects found
                if error == nil || error?.code == 11 {

                    var usernameExists = false

                    for record in records! {

                        if record.objectForKey("username") as? String == self.usernameField.text {
                            //other user with the same username exists - don't allow user to create account
                            usernameExists = true

                        }

                    }

                    if usernameExists == true {

                        //displayErrorAlert is a function in which I display an error alert to the user
                        displayErrorAlert("Username \(self.usernameField.text!) is taken. Please choose another one.", vc: self)

                    } else {
                        //user can sign up
                        let record = CKRecord(recordType: "MyUsers")
                        record.setObject(self.usernameField.text!, forKey: "username")
                        record.setObject(self.passwordField.text!, forKey: "password")

                        pubDB.saveRecord(record, completionHandler: { (record, error) in

                            if error == nil {

                                NSOperationQueue.mainQueue().addOperationWithBlock {

                                    NSUserDefaults.standardUserDefaults().setObject(self.usernameField.text!, forKey: "username")
                                    username = self.usernameField.text!
                                    self.performSegueWithIdentifier("account", sender: self)

                                }

                            } else {

                                print(error)

                            }

                        })

                    }

                } else {

                    print(error)

                }

            })

Here is the code for logging in:

//query users to find current user
let query = CKQuery(recordType: "MyUsers", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
            pubDB.performQuery(query, inZoneWithID: nil, completionHandler: { (records, error) in

                //we do not need to check for error code 11 because a user should exist
                if error == nil {

                    var userExists = false

                    for record in records! {

                        if record.objectForKey("username") as? String == self.usernameField.text {

                            if record.objectForKey("password") as? String == self.passwordField.text {

                                NSOperationQueue.mainQueue().addOperationWithBlock {

                                    userExists = true
                                    NSUserDefaults.standardUserDefaults().setObject(self.usernameField.text!, forKey: "username")
                                    username = self.usernameField.text!
                                    self.performSegueWithIdentifier("account", sender: self)

                                }

                            } else {

                                //user with the username exists, but the password does not match
                                displayErrorAlert("Your password is incorrect", vc: self)

                            }

                        }

                    }

                    if userExists == false {

                        //user with that username does not exist
                        displayErrorAlert("Your username is incorrect", vc: self)

                    }


                } else {

                    print(error)

                }

            })

        }

When the view appears, I call this code to check for a current user:

if NSUserDefaults.standardUserDefaults().objectForKey("username") != nil {
    username = NSUserDefaults.standardUserDefaults().objectForKey("username") as! String
    self.performSegueWithIdentifier("account", sender: self)
}

The variable username is a global variable for all classes. If you do not prefer to user global variables, you should change it.

Hope this helps.



来源:https://stackoverflow.com/questions/36232100/how-to-create-user-in-cloudkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!