[Swift in Xcode]Using parse to implement signup and signin

2016-01-24

For me, I love to use Parse as the database for App. It’s more flexible than MySQL I think, and it has already had many api for us to use. In this article, I am going to illustrate how to use parse to implement signup and signin. 🙂 I use swift and Xcode to starting programming for my project.Basically, I will talk about these three sections:

Get to Parse

Here is the parse website–Parse, after you sign in, you can create a new App in the dashboard.
p
Then hover your mouse to the app you have just created. Click Quickstart. Then click social->mobile->IOS->Swift, then if you have already got an existing project then choose the existing project, if not, choose new project. For the new project, you do not need to do any setup. So for here, I will illustrate how to setup with the existing project.

  1. Download & unzip the SDK
  2. Add the SDKS to your app.
    You can just drag the Parse.framework and Bolts.framework you downloaded into your Xcode project folder target. Make sure the “Copy items to destination’s group folder” checkbox is checked. However, for me, I always drag all the framework into that.
    There is one tip: If you create a new group and put all that framework into the new group, remember to change your framework path. If you do not do that, you can not import parse!
  3. Add the dependencies. Just click on targets -> Your app name -> and then the ‘Build phases’ tab. Expand ‘Link Binary with Libraries’ as shown. The libraries which need to be added have shown on the parse website.
    Then connect your app to parse, do the same step like the website tutorial, I will not explain more about that. One thing still need to pay attention again is “import Parse”. If you come with an error called, no such module parse, the reason will be the path for the framework!

Create signUp Action

Then basic idea for this part is insert our user information to the database. Generally, in our Main.storyboard, we create some labels and textfields then we control drag that to our cocoa touch class. Here is an example code for register button action:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
@IBAction func register(sender: AnyObject) {
if(passwordTF.text == passwordAgainTF.text){
if(usernameTF.text != "" && passwordTF.text != "" && passwordAgainTF.text != "" && emailTF.text != ""){
let user = PFUser()
user.username = usernameTF.text
user.password = passwordTF.text
user.email = emailTF.text
user.signUpInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if (error == nil) {
// Hooray! Let them use the app now.
let success = UIAlertController(title: "Error", message: "You have registered successfully!", preferredStyle: .Alert)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let ok = UIAlertAction(title: "OK", style: .Default, handler: nil)
success.addAction(cancel)
success.addAction(ok)
self.presentViewController(success, animated: true, completion: nil)
} else {
// Examine the error object and inform the user.
let emailError = UIAlertController(title: "Error", message: "Your email address is wrong", preferredStyle: .Alert)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let ok = UIAlertAction(title: "OK", style: .Default, handler: nil)
emailError.addAction(cancel)
emailError.addAction(ok)
self.presentViewController(emailError, animated: true, completion: nil)
}
}
}
else{
let emptyError = UIAlertController(title: "Error", message: "You must fill all the blankets", preferredStyle: .Alert)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let ok = UIAlertAction(title: "OK", style: .Default, handler: nil)
emptyError.addAction(cancel)
emptyError.addAction(ok)
self.presentViewController(emptyError, animated: true, completion: nil)
}
}
else{
let passwordError = UIAlertController(title: "Error", message: "The password you type is not the same", preferredStyle: .Alert)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let ok = UIAlertAction(title: "OK", style: .Default, handler: nil)
passwordError.addAction(cancel)
passwordError.addAction(ok)
self.presentViewController(passwordError, animated: true, completion: nil)
}
}

Inside this code, I have also created some validation for password and non-empty blankets. Remember, in parse, the password will be hidden to protect the user information. And the email has already have the validate check. So we do not need to write again.

Create SignIn Action

Here is the code for login, the advantage for parse is that it has already got a logInWithUsernameInBackground. So that’s why I love it 🙂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@IBAction func logInPress(sender: UIButton) {
if(userNameTF.text != "" && passwordTF.text != ""){
let user = PFUser()
user.password = passwordTF.text
user.username = userNameTF.text
PFUser.logInWithUsernameInBackground(userNameTF.text!, password: passwordTF.text!, block: {
(User: PFUser?, Error : NSError?)->Void in
if Error == nil{
dispatch_async(dispatch_get_main_queue()){
print("success log in")
}
}
})
}
else{
let emptyError = UIAlertController(title: "Error", message: "You must fill all the blanckets!", preferredStyle: .Alert)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let ok = UIAlertAction(title: "OK", style: .Default, handler: nil)
emptyError.addAction(cancel)
emptyError.addAction(ok)
self.presentViewController(emptyError, animated: true, completion: nil)
}
}

Thanks for reading it. XD I learned this tips from my Group project, however, I cannot shared the source code of all my project now. 🙁 Maybe after the demo day of my project, I will publish that to gitHub. 🙂 If you have any questions, feel free to contact with me.XD

Important Note:

Sad news: the hosted version of Parse will be fully retired on January 28th, 2017. So I have posted another post about using MYSQL for swift. Please check that.

Comments: