What is the ideal way to exit an iOS app?

The code below works, but has an error. The scenario is that I start by logging in to the application. After successful login, the application will install UserDefaults (UserId). After that, I can navigate through the types of applications with the UserId saved. As soon as I go to the settings and go to the bookmark, this will clear the UserId and go into login mode.

ERROR: When I enter the application again and press the home button to go to the iPhone’s desktop, close the application and return it to open it again, and it still saves the UserId. So, if I go to setup and exit the system, which will clear the UserId and will not enter the login mode. I do not know why.

Code:

- (IBAction)resetKeychain:(id)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Are you sure you want to logout?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Logout" otherButtonTitles:nil]; actionSheet.actionSheetStyle = UIActionSheetStyleDefault; [actionSheet showFromTabBar:self.tabBarController.tabBar]; [actionSheet release]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex ==0) { //logout NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; //delete user ID fro user Defaults [defaults setObject:nil forKey:@"UserId"]; //redirect to login view NewClassMoonAppDelegate * appsDelegate =[[UIApplication sharedApplication] delegate]; [appsDelegate.window addSubview:[appsDelegate.login view]]; } } 
+4
source share
3 answers

From what I can interpret from you, the question that needs to be formatted and made coherent, I believe that:

a) your value @"UserID" not synchronized with NSUserDefaults because you are not calling the -synchronize method. NSUserDefaults update the storage of key values ​​in memory, but will not write it to disk, which means that it is lost at any time.

b) The fact that it does not fit loginView may be due to several reasons, most likely, this is already a subheading of your UIWindow . So, instead of reusing the login property in the application’s deletion, create a new View Controller instance variable and set rootViewController instead.

 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:nil forKey:@"UserId"]; [defaults synchronize]; //redirect to login view NewClassMoonAppDelegate * appsDelegate =[[UIApplication sharedApplication] delegate]; LoginViewController *login = [[LoginViewController alloc] initWithNibName...]; [appsDelegate.window setRootViewController:nil]; [appsDelegate.window setRootViewController:login]; } } 
+8
source

Swift

From max_ response with some changes:

 let objectsToSave: [String] = [obj1, obj2, obj3] for key in objectsToSave { NSUserDefaults.standardUserDefaults().removeObjectForKey(key) } let appsDelegate = UIApplication.sharedApplication().delegate let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil) let newLoginVC: LoginViewController = storyboard.instantiateInitialViewController() as! LoginViewController appsDelegate?.window!!.rootViewController = nil appsDelegate?.window!!.rootViewController = newLoginVC 

EDIT: The only changes I made are that I initialize newLoginVC via Storyboards instead of initWitName.

+1
source

Make a property of the UINavigation controller and synthesize it, and write the code below on the exit button

 AppDelegate * appDelegateObj =[[UIApplication sharedApplication] delegate]; login *loginObj = [[login alloc]initWithNibName:@"login" bundle:nil]; [appDelegateObj.window setRootViewController:nil]; navObj=[[UINavigationController alloc]initWithRootViewController:loginObj]; [appDelegateObj.window setRootViewController:navObj]; 
0
source

All Articles