从IOS应用程序注销的最佳方法是什么? [英] What is the perfect way to make a logout from IOS app?

查看:129
本文介绍了从IOS应用程序注销的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可正常运行,但存在错误.场景是,我首先登录以进入应用程序系统.登录成功后,应用程序将设置UserDefaults(UserId).之后,我可以使用存储的UserId导航应用程序视图.转到设置和选项卡注销后,将清除UserId并进入登录视图.

The below code is working but has a bug. The scenario is that, I begin by logging in to enter the app system. Once the login has succeeded, the app will set UserDefaults (UserId). After that, I can navigate the app views with stored UserId. Once I go to settings and tab logout, that will clean UserId and go to login view.

错误:当我再次登录该应用程序并单击主页"按钮以转到iPhone桌面并关闭该应用程序后,再次打开以返回该窗口,它仍存储UserId.因此,如果我转到该设置并注销,将清除UserId并且不会进入登录视图.我不知道为什么.

The BUG: When I login again to the app and click the home button to go to iPhone desktop and close the app, and return to open it again it still storing the UserId. So, if I go to the setting and log out that will clean UserId and will not go to login view. I don't know why.

代码:

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

    }
}

推荐答案

根据我可以解释的问题,我需要对它进行格式化并使其连贯,我相信:

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

a)您的@"UserID"值未与NSUserDefaults同步,因为您没有调用-synchronize方法. NSUserDefaults将更新其内存中键值存储,但不会将其写入磁盘,这意味着它会在任意时间丢失.

a) your @"UserID" value is not syncing with NSUserDefaults because you are not calling the -synchronize method. NSUserDefaults will update its in-memory key-value store, but will not write it to disk meaning that it's lost at an arbitrary time.

b)它不进入loginView的事实可能与某些原因有关,最有可能的原因是它已经是您的UIWindow的子视图.因此,不要在应用程序委托中重用login属性,而是创建一个新的View Controller实例变量,并将其设置为rootViewController.

b) The fact that it is not going to the loginView could be to do with any few reasons, most likely that it is already a subview of your UIWindow. So, instead of reusing the login property in your app delegate, create a new instance variable of the View Controller, and set that as the 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];

    }
}

这篇关于从IOS应用程序注销的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆