简体   繁体   English

持久登录 iOS Objective-C

[英]Persistent login iOS Objective-C

I am writing an app in Objective-C, and I have an API correctly supplying me access tokens to keep a persistent login state. If a user logs out, their token is invalidated and no longer exists in the keychain.我正在 Objective-C 中编写一个应用程序,我有一个 API 正确地为我提供访问令牌以保持持久登录 state。如果用户注销,他们的令牌将失效并且不再存在于钥匙串中。 However, what is the best way for me to check this upon the user opening the app?但是,我在用户打开应用程序时检查它的最佳方式是什么?

Essencially, I need to check is the user logged in?本质上,我需要检查用户是否登录? if so, check & potentially refresh their access token, and take them to 'X' screen.如果是这样,检查并可能刷新他们的访问令牌,并将他们带到“X”屏幕。 If they are not, take them to the login screen.如果不是,请将它们带到登录屏幕。

Currently, I have this in the AppDelegate.m in the didFinishLaunchingWithOptions function like so:目前,我在didFinishLaunchingWithOptions function 的AppDelegate.m中有这个,如下所示:

- (void) checkLoginState {
    // Finally, is the user logged in or not?
    NSString * storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kLoggedInState]) { // if apparently logged in,
        userService = [[UserDataService alloc] init];
        [userService getUserAccountInformation:^(NSURLSessionTask * _Nonnull task, id  _Nonnull responseObject) {
            UIViewController * vc;
            if ([[NSUserDefaults standardUserDefaults] boolForKey:kCheckedInStatus]) {
                vc = [storyboard instantiateViewControllerWithIdentifier:@"TABBAR_ID"];
            } else {
                vc = [storyboard instantiateViewControllerWithIdentifier:@"VENUE_NAV_ID"];
            }
            vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            [[self window].rootViewController presentViewController:vc animated:YES completion:nil];
        } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
            // Save state  to NSUser defualts
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kLoggedInState];
            [[NSUserDefaults standardUserDefaults] synchronize];

            UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"LOGIN_ID"];
            vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            [[self window].rootViewController presentViewController:vc animated:YES completion:nil];
        }];
    } else {
        // to logout
        // Save state to NSUser defaults
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kLoggedInState];
        [[NSUserDefaults standardUserDefaults] synchronize];

        UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"LOGIN_ID"];
        vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [[self window].rootViewController presentViewController:vc animated:YES completion:nil];
    }

} }

This to me seems quite bulky, and im not sure if this is best practice for such an approach.这对我来说似乎很笨重,我不确定这是否是这种方法的最佳实践。 Is there any advice to this?对此有什么建议吗? Even any cleaner way to write this code?甚至还有更简洁的方法来编写这段代码吗?

User information is stored locally, and the corresponding page can be skipped according to whether the user information exists.用户信息保存在本地,可以根据用户信息是否存在跳转相应页面。 For possible user logout events, the back end should add the corresponding logout code in all interfaces that require user permissions, receive the code code itself, and clear the user information and jump to the login interface.对于可能出现的用户登出事件,后端要在所有需要用户权限的界面中添加相应的登出代码,自己接收code代码,并清除用户信息并跳转到登录界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM