Reputation: 119
Whenever I first provision my app on my phone, whichever user I login as, thats the username that stays logged in. Even when I terminate my app and bring it back up, the login screen comes back up, but regardless of what credentials I put in, it is the original username that I first used when the app was first provisioned that is truly logged in (my SQL databases confirm this).
Is there anyway to terminate the user session and log them out when someone terminates the app (not hits the "home" button, but literally closes the app from running in the background)?
Likewise, once I figure this out, is there anyway to NOT bring up my login view controller if the app is still running in the background of their device and has not been fully terminated?
The exact code is preferred as an answer...
Upvotes: 1
Views: 2040
Reputation: 606
I recommend you to see UIApplicationDelegate
. There is many interesting delegation method to deal with your application state. Of course , you can implement these method in any ViewController. Let's look up there.
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
Your question:
Is there anyway to terminate the user session and log them out when someone terminates the app (not hits the "home" button, but literally closes the app from running in the background)?
Of course, you can implement the method to clear the user session and log them out (If you do in single pattern, it will be great to handle with these situation in any ViewController)
Likewise, once I figure this out, is there anyway to NOT bring up my login view controller if the app is still running in the background of their device and has not been fully terminated?
You should handle with 3 steps from my answer in other StackOverflow question here.
You can see also useful information in UIApplicationDelegate Protocol Reference.
Upvotes: 2
Reputation: 7471
do log out process in following delegate method. It may help you..
`- (void)applicationWillTerminate:(UIApplication *)application`
Upvotes: 0