1
votes

I'm working on a swift iPhone application using Parse for the backend and when I restart the app the currentUser isn't recognized and returns nil redirecting to the login page. Once I sign back in the user stays logged in until I stop the app.

if PFUser.currentUser() == nil {
  //return to the login page 
} else {
  // perform normal operations
}

I'm curious why this is happening since the only place I have PFUser.logOut() is in a logout function which is not on the initial view controller and only called when a button is pressed. Thanks in advance for the help!

EDIT: This problem began occurring this morning. The past 2 weeks I haven't had any issues regarding automatic login with parse. So I don't know what I could have done to cause it. The only code I changed between last night and this morning was trying to pass data from one view controller to a popoverViewController by adding delegates to my VC's but I have since deleted them and am still at a loss.

2
what do you mean by restarting the app? - Lamour
Do you use Parse local datastore? - egor.zhdan
By restarting the app I mean killing it and any background processes and then opening it back up. And no I'm not using the local datastore for anything. - nviens
I am not using the local datastore but it IS enabled - nviens
Sounds like you didnt set the currentUser on login. - Devster101

2 Answers

1
votes

I ended up downloading the latest Parse SDK and it solved the problem. Eddy from this post PFUser currentUser nil after app update also said reverting to a previous Parse SDK also solved the issue.

0
votes

I think you should place this code in the AppDelegate into the method applicationDidBecomeActive(), something like the following:

 func applicationDidBecomeActive(application: UIApplication) 
 {

    if PFUser.currentUser() == nil{
        // show main screen

        var storyboard :UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        var LoginScreen = storyboard.instantiateViewControllerWithIdentifier("loginShow") as! UIViewController
        UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(LoginScreen, animated: true, completion: nil)
    }

    else 
    {
        //show login screen
        var storyboard :UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        var MainScreenNavigation = storyboard.instantiateViewControllerWithIdentifier("TotalMainViewController") as! UIViewController
        UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(MainScreenNavigation, animated: false, completion: nil)

    }

}