初次登录后如何自动登录用户?

时间:2015-11-25 22:36:35

标签: ios swift parse-platform

用户登录后即使应用程序终止,如何让他们保持登录状态?目前用户可以登录,但是当他们结束应用程序时,他们必须再次登录。相反,我希望他们即使应用程序结束也要登录。以下是用户登录的代码。

 @IBAction func loginAction(sender: AnyObject) {

    let username = self.usernameField.text
    let password = self.passwordField.text


    if(username!.utf16.count) < 4 || (password!.utf16.count < 5){

    let alert = UIAlertView(title: "Invalid", message: "Username must be greater then 4 and Password must be greater then 5", delegate: self, cancelButtonTitle: "OK")
    alert.show()

    }else{

        self.actInd.startAnimating()

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

            self.actInd.stopAnimating()

            if ((user) != nil) {

                //var alert = UIAlertView(title: "Success", message: "Logged In", delegate: self, cancelButtonTitle: "OK")
                //alert.show()



                dispatch_async(dispatch_get_main_queue(),{

                    self.performSegueWithIdentifier("login", sender: self)


                })

            }else {

                let alert = UIAlertView(title: "Error", message: "\(error)", delegate: self, cancelButtonTitle: "OK")
                alert.show()

            }



        })

    }


}

1 个答案:

答案 0 :(得分:0)

您是否在再次启动应用后检查了PFUser.currentUser()

viewDidAppear中的示例:

let currentUser = PFUser.currentUser()

if currentUser == nil {
     // log in logic

} else {
     // user is logged in

}