1

I am attempting to keep the current user session active until the user decides to log out. What code i should i implement? and exactly where should it be implemented? the viewDidLoad, or viewWillAppear function of the root view? I have tried using this code in my root view:

override func viewDidAppear(animated: Bool) {
        let vc = ViewController()
        var loggedIn = false

        if PFUser.currentUser() != nil {
            loggedIn = true
        } else {
            presentViewController(vc, animated: true, completion: nil)
        }
    }

but whenever i stop the simulator and run it again, i have to log in all over again. What is the best solution? keep in my "ViewController" is my main view which holds my login/sign up fields. So i basically want a way to say if a current user session exist, continue, else, show initial view.

Matteo Piombo
  • 6,688
  • 2
  • 25
  • 25
Mike Strong
  • 940
  • 3
  • 13
  • 31

3 Answers3

2

Try this in your app delegate...

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

...

PFUser *currentUser = [PFUser currentUser];

if (currentUser) {
  // there is a user logged in go to the main screen
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    MainViewController *mainViewController = (MainViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Main"];

    //set the root controller to it
    self.window.rootViewController = mainViewController;
} else {
// Log in!
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Login" bundle: nil];
    LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];

    //set the root controller to it
    self.window.rootViewController = loginViewController;
}

...
fechidal89
  • 723
  • 5
  • 25
  • i tagged swift, not ob j.. if you can use swift syntax i can definitely try this – Mike Strong Jul 07 '15 at 05:33
  • @MikeStrong I'm sorry for using objective-c, [this can help you](http://stackoverflow.com/questions/25474115/how-to-load-uiviewcontroller-programmatically-from-storyboard). Is simply load a view controller programmatically – fechidal89 Jul 07 '15 at 05:39
  • This actually worked however it must be placed in the viewDidLoad function of the initial view not the app delegate. so if you adjust your answer i can accept it! thanks – Mike Strong Jul 07 '15 at 06:11
  • @MikeStrong, The [documentation](https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingViewControllersinYourApplication/UsingViewControllersinYourApplication.html) refers to this: _Your app delegate is called to configure the initial view controller before it is displayed_, and in my case I use .xib for launch screen. I don't believe load of a vc for load other vc is efficient if exist a user session. Thanks! – fechidal89 Jul 07 '15 at 06:44
0

Parse is handling this for you when you register user properly with signUpInBackgroundWithBlock (objective -c).

PFUser.currentUser() should be available all the time until you actively log out.

maybe you try to register -> fail -> go to next VC anyway -> start the app again without being actually registered ?

ozd
  • 1,194
  • 10
  • 34
0

Parse has provided developers with Sessions (for session management). for further help refer to Sessions Section in following link... https://parse.com/docs/ios/guide

Waseem Lateef
  • 151
  • 1
  • 6