简体   繁体   中英

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. 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. If they are not, take them to the login screen.

Currently, I have this in the AppDelegate.m in the didFinishLaunchingWithOptions function like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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