xxtmanxx
xxtmanxx

Reputation: 209

How do I get the program to perform the segue after loading user info?

I have a login screen with the username and password. I can currently load the username and password after the app is closed and opened back again, but I have to click the login button, instead I want it to perform the segue if the credentials are correct. I am using NSUserDefault to remember the credentials. Below is my code.

override func viewDidLoad() {
    super.viewDidLoad()

    if let usernameIsNotNill = defaults.objectForKey("username") as? String {
        self.usernameField.text = defaults.objectForKey("username") as? String
    }

    if let passwordIsNotNill = defaults.objectForKey("password") as? String {
        self.passwordField.text = defaults.objectForKey("password") as? String
    }


    // Do any additional setup after loading the view.
}
 @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()
                var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
                defaults.setObject(self.usernameField.text, forKey: "username")
                defaults.setObject(self.passwordField.text, forKey: "password")
                defaults.synchronize()


                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()

            }



        })

    }


}

Upvotes: 0

Views: 108

Answers (3)

rnsjtngus
rnsjtngus

Reputation: 225

Fisrt, check that user id and password are stored.

If they stored in device, check and push next viewcontroller.

For example,

let userDefaults = NSUserDefaults.standardUserDefaults();
if let userDefaults.objectForKey("username"){
    if let userDefaults.objectForKey("password"){
        // do login process
        // if user id and password are valid then
        let nextViewController = YourViewController();
        self.presentViewController(nextViewController, animated: true, completion: nil);
    }
}
// else alert some message and stay in loginViewController

Upvotes: 0

Christos Chadjikyriacou
Christos Chadjikyriacou

Reputation: 3759

override func viewDidAppear() {}

This function will excecute when the view is on the screen. So in this method go check for the credentials in NSUserDefaults. If theres some info in there excecute this

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()

        }



    })

But the username and password variables should be the ones from NSUserDefaults

Upvotes: 0

pbush25
pbush25

Reputation: 5248

You should not be storing a user's password in standard user defaults as plain text. If you're not writing an application that requires web access, then there's no point in making them log in really, and thus you wouldn't need to implement this. If a user needs an account for your app, I would suggest implementing one through a backend such as Parse which will happily handle the password more securely than you will.

Upvotes: 1

Related Questions