问题
With the following code I am able to logout successfully and show the login screen when the application is opened again.
I have a user profile page where I fetch all the data of a particular user. However, a problem occurs after logging in. I get all the user's data. Then when I minimise the application and open it again, all the data in my profile page is lost. I think this is because I am setting the user id to nil
with setObject
method.
Does anyone have any idea about how to make this work?
- (IBAction)logoutButtonTapped:(id)sender {
UIAlertView* alert = [[UIAlertView alloc]
initWithTitle:@"Confirmation"
message:@"Do you want to Logout?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[alert show];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:nil forKey:@"userID"];
[prefs synchronize];
}
Below is the code I am using to LOGOUT:
-(void)nextScreen: (NSTimer *) timer
{
NSLog(@"TEST");
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
MenuScreenViewController *viewcontroller = [storyboard instantiateViewControllerWithIdentifier:@"loginScreenViewController"];
[self.navigationController pushViewController:viewcontroller animated:YES];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(nextScreen:)
userInfo:nil
repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[timer fire];
if (buttonIndex != 0) // 0 == the cancel button
{
//home button press programmatically
UIApplication *app = [UIApplication sharedApplication];
[app performSelector:@selector(suspend)];
//wait 2 seconds while app is going background
[NSThread sleepForTimeInterval:2.0];
//exit app when app is in background
NSLog(@"TIMER CALLED");
exit(0);
}else{
}
回答1:
you can take one bool
variable which is stored login status. If successfully logged in then store status to yes
to NSUserdefaults
. And store NO
when use click logout
.
So, you can check at a time of app launching
that if status is true then rootviewcontroller
is homescreen
else logingscreeen
Don't manipulate userId
so you can use it. Use this bool status.
Second thing If you showing alertview for confirming on logout button click then don't manipulate NSUserdefaults
in that action method. Because if user will press cancel
then also your user default
will be change and consider user as logout
.
So, you should use alertview delegate
methods to handle click on alertview
. You should manipulate user defaults
from ok
button's click of alertview
which can be achieved by delegate methods.
Hope this will help :)
回答2:
When you call [alert show]
, it seems like you're assuming that it stops executing your method there and only continues if the user taps OK. It doesn't work that way. What happens is that you're showing the alert view, your method continues, then sets your userID
to nil
immediately, before you tap any button. You then tap on a button in the alert view, and the information about what you tapped is thrown away.
If you want to log somebody out only if they tap OK, then you need to implement the UIAlertViewDelegate
protocol, and set your userID
to nil
in the delegate method if the right button is tapped.
Note that UIAlertView
is deprecated, and you should be using UIAlertController
in new code.
来源:https://stackoverflow.com/questions/37658504/how-to-logout-without-setting-userid-nil