프로그래밍/IOS

[IOS] 파스 회원가입, 로그인 : Log in and Sign up

Gilvert 2016. 1. 3. 02:27
728x90

#1. 목표

- 아이디 & 비밀번호 등록과 로그인 진행을 해보자


#2. 정리

0. AppDelegate 값에서  application id 와 Client key 등록

- Parse.setApplicationId("application id"clientKey: "client key")

1. 아이디와 비밀번호 없이 sign up 버튼을 누르면 경고창 나옴

2. if~ else 값이 회원등록과 로그인 2개로 크게 나뉨

3. [회원등록] 아이디와 비밀번호 등록하고 로그인 하면 스피너 팝업

4. [회원등록PFUser() 객체 생성, 아이디, 비밀번호 집어넣음

- signUpInBackgroundWithBlock()

- 성공하게 되면 스피너 동작 중지

- 에러 인 경우 에러 메시지 출력

5. [로그인PFUser.logInWithUsernameInBackground()

-  에러메시지 받아서 알림창 출력

6. Bool 값을 이용해서 Sign up과 Log in 값을 서로 변경함



#3. 소스


import UIKit

import Parse


class ViewController: UIViewController {

    

    var signupActive = true

   

    @IBOutlet var username: UITextField!

    

    @IBOutlet var password: UITextField!

    

    @IBOutlet var signupButton: UIButton!

    

    @IBOutlet var registeredText: UILabel!

    

    @IBOutlet var loginButton: UIButton!

    

    var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

    

    func displayAlert(title: String, message: String) {

        

        var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction((UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in

            

            self.dismissViewControllerAnimated(true, completion: nil)

            

        })))

        

        self.presentViewController(alert, animated: true, completion: nil)

        

        

    }

    

    @IBAction func signUp(sender: AnyObject) {

        

        if username.text == "" || password.text == "" {

            

            displayAlert("Error in form", message: "Please enter a username and password")

            

        } else {

            

            activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))

            activityIndicator.center = self.view.center

            activityIndicator.hidesWhenStopped = true

            activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray

            view.addSubview(activityIndicator)

            activityIndicator.startAnimating()

            UIApplication.sharedApplication().beginIgnoringInteractionEvents()

            

            var errorMessage = "Please try again later"

            

            if signupActive == true {

            

            var user = PFUser()

            user.username = username.text

            user.password = password.text

            

            

            

            user.signUpInBackgroundWithBlock({ (success, error) -> Void in

                

                self.activityIndicator.stopAnimating()

                UIApplication.sharedApplication().endIgnoringInteractionEvents()

                

                if error == nil {

                    

                    // Signup successful

                    

                    

                    

                } else {

                    

                    if let errorString = error!.userInfo["error"] as? String {

                        

                        errorMessage = errorString

                        

                    }

                    

                    self.displayAlert("Failed SignUp", message: errorMessage)

                    

                }

                

            })

                

            } else {

                

                PFUser.logInWithUsernameInBackground(username.text!, password: password.text!, block: { (user, error) -> Void in

                    

                    self.activityIndicator.stopAnimating()

                    UIApplication.sharedApplication().endIgnoringInteractionEvents()

                    

                    if user != nil {

                        

                        // Logged In!

                        

                    } else {

                        

                        if let errorString = error!.userInfo["error"] as? String {

                            

                            errorMessage = errorString

                            

                        }

                        

                        self.displayAlert("Failed Login", message: errorMessage)

                        

                    }

                    

                })

                

            }

            

        }

            

        

        

    }

    

    

    @IBAction func logIn(sender: AnyObject) {

        

        if signupActive == true {

            

            signupButton.setTitle("Log In", forState: UIControlState.Normal)

            

            registeredText.text = "Not registered?"

            

            loginButton.setTitle("Sign Up", forState: UIControlState.Normal)

            

            signupActive = false

            

        } else {

            

            signupButton.setTitle("Sign Up", forState: UIControlState.Normal)

            

            registeredText.text = "Already registered?"

            

            loginButton.setTitle("Login", forState: UIControlState.Normal)

            

            signupActive = true

            

        }

        

        

    }

    

    override func viewDidLoad() {

        super.viewDidLoad()

           }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

가우넷 구름 USB 충전식 양면 대용량 보조배터리 손난로, 단일 상품, 화이트



"파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있음"