【问题标题】:How to logout without setting "userID" nil如何在不设置“userID”的情况下注销 nil
【发布时间】:2016-10-06 02:59:38
【问题描述】:

使用以下代码,我可以成功注销并在再次打开应用程序时显示登录屏幕。

我有一个用户个人资料页面,我可以在其中获取特定用户的所有数据。但是,登录后出现问题。我得到了所有用户的数据。然后,当我最小化应用程序并再次打开它时,我的个人资料页面中的所有数据都丢失了。我认为这是因为我使用setObject 方法将用户ID 设置为nil

有人知道如何进行这项工作吗?

- (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];

}

以下是我用于注销的代码:

  -(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{

     }

【问题讨论】:

  • 有人知道我应该在 else 方法中使用什么来在同一视图上或关闭警报视图

标签: ios objective-c login logout


【解决方案1】:

您可以使用一个bool 变量来存储登录状态。如果成功登录,则将状态存储到yesNSUserdefaults。并在使用点击logout时存储NO。 因此,您可以在 time of app launching 处检查,如果状态为真,则 rootviewcontrollerhomescreen 否则为 logingscreeen

不要操纵userId,以便您可以使用它。使用此布尔状态。

第二件事如果您显示警报视图以确认注销按钮单击,则不要在该操作方法中操作NSUserdefaults。因为如果用户按下cancel,那么您的user default 也将被更改并将用户视为logout

因此,您应该使用alertview delegate 方法来处理点击alertview。您应该通过ok 按钮对alertview 的点击来操作user defaults,这可以通过委托方法来实现。

希望这会有所帮助:)

【讨论】:

  • 感谢您的回答,但您能用代码解释一下吗?我不知道该怎么做@Lion
【解决方案2】:

当您调用[alert show] 时,您似乎假设它在那里停止执行您的方法,并且只有在用户点击OK 时才会继续执行。它不是那样工作的。发生的情况是您正在显示警报视图,您的方法继续,然后在您点击任何按钮之前将您的userID 设置为nil 立即。然后,您在警报视图中点击一个按钮,有关您点击的内容的信息将被丢弃。

如果您只想在某人点击 OK 时将其注销,那么您需要实现UIAlertViewDelegate 协议,并在委托方法中将您的userID 设置为nil,如果点击右键。

请注意,UIAlertView 已弃用,您应该在新代码中使用 UIAlertController

【讨论】:

  • 我使用了委托方法,代码未在此处发布
  • 但是你用来登出的代码贴在这里的,而且放错地方了。你需要把它放在委托方法中。
  • 查看代码并建议我最后应该使用什么,如果我按下取消按钮并想要关闭警报或留在同一页面上@Jim
  • logoutButtonTapped: 方法的最后三行放入委托方法中的if 语句中。目前尚不清楚您在该方法中还做了什么,但 Apple 禁止手动退出此类应用程序。
  • 好的,我明白了,但是告诉我应该在 else 方法中写什么如何在按下取消按钮时什么都不做@Jim
猜你喜欢
  • 2022-11-18
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多