如何在iPhone应用程序终止时注销用户?

时间:2012-05-06 15:30:55

标签: iphone xcode cocoa-touch

每当我第一次在我的手机上配置我的应用程序时,无论我登录哪个用户,都会保留登录的用户名。即使我终止我的应用程序并重新启动它,登录屏幕也会重新启动,但不管是什么我输入的凭据,它是我第一次使用该应用程序时首次使用的原始用户名,该用户名是真正登录的(我的SQL数据库确认了这一点)。

是否有终止用户会话并在有人终止应用时将其注销(不是点击“主页”按钮,但实际上是关闭应用程序在后台运行)?

同样,一旦我弄明白这一点,如果应用程序仍然在他们的设备后台运行并且尚未完全终止,那么无论如何都不会启动我的登录视图控制器吗?

确切的代码是首选答案......

2 个答案:

答案 0 :(得分:1)

我建议你看UIApplicationDelegate。有许多有趣的委托方法来处理您的应用程序状态。当然,您可以在任何ViewController中实现这些方法。让我们看看那里。

- (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:.
     */
}

您的问题:

  

是否有终止用户会话并在有人终止应用时将其注销(不是点击“主页”按钮,但实际上是关闭应用程序在后台运行)?

当然,您可以实现该方法来清除用户会话并将其注销(如果您使用单一模式,那么在任何ViewController中处理这些情况都会很棒)

  

同样,一旦我弄明白这一点,如果应用程序仍然在他们的设备后台运行并且尚未完全终止,那么无论如何都不会启动我的登录视图控制器吗?

您应该从我在其他StackOverflow问题here的答案中处理3个步骤。

您还可以在UIApplicationDelegate Protocol Reference中看到有用的信息。

答案 1 :(得分:0)

在以下委托方法中注销进程。它可能对你有帮助..

   `- (void)applicationWillTerminate:(UIApplication *)application`