iOS: How to authenticate a user after login (for auto login)?

前端 未结 4 1390
醉酒成梦
醉酒成梦 2021-02-03 11:16

I\'d like to use an auto-login function. So when the user opens the app, he gets delegated to a \"login screen\". When he logged in successfully he should be directed to his acc

4条回答
  •  旧时难觅i
    2021-02-03 12:02

    I used a combination of NSUserDefaults and SSKeychain. I used NSUserDefaults to store the username nad SSKeychain to store the password.

    This is the code I used to save the credentials

    NSString *user = self.username.text;
    NSString *password = self.pass.text;
    [SSKeychain setPassword:password forService:@"achat" account:user];
    NSUserDefaults *dUser = [NSUserDefaults standardUserDefaults];
    [dUser setObject:user forKey:@"user"];
    [dUser synchronize];
    

    This is the code I used to retrieve the credentials

    NSUserDefaults *eUser = [NSUserDefaults standardUserDefaults];
    NSString *savedUser = [eUser objectForKey:@"user"];
    
        if (!savedUser) {
            UIAlertView *uhoh = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Please enter your username and password." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [uhoh show];
        }
    
        else {
                NSString *savedPass = [SSKeychain passwordForService:@"achat" account:savedUser];
                self.username.text = savedUser;
                self.pass.text = savedPass;
             }
    

提交回复
热议问题