jckly
jckly

Reputation: 851

SSKeychain credential store not being set properly?

I have a weird bug with SSKeychain's credentialstore in my iOS app. When a user logs in I store a bunch of info in the credential store. But recently the credential store is not setting the items.

What's weird is, it doesn't happen all the time. Sometimes it sets the information and works as expected.. other times the credential store values are returning nil after login. My code looks like this:

- (void)login:(id)sender {
    [SVProgressHUD show];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager setResponseSerializer:[LoginResponseSerializer serializer]];

    __weak typeof(self)weakSelf = self;

    NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kLoginURL];

    if (self.passwordField.text && self.emailField.text) {
        NSDictionary *params = @{ @"email": self.emailField.text, @"password": self.passwordField.text};

        self.sessionManager = manager;
        [manager POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            // prgress implementation
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *user = [responseObject objectForKey:@"user"];



            NSString *token = user[@"auth_token"];
            NSString *avatarURL = user[@"avatar_url"];
            NSString *userID = [NSString stringWithFormat: @"%@", user[@"id"]];
            NSString *firstName = user[@"first_name"];
            NSString *lastName = user[@"last_name"];
            NSString *levelOfStudy = user[@"level"];

            BOOL teecher = [user[@"teecher"] boolValue];

            [weakSelf.credentialStore setTeecher:teecher];
            [weakSelf.credentialStore setUserId:userID];
            [weakSelf.credentialStore setAvatarURL:avatarURL];
            [weakSelf.credentialStore setAuthToken:token];
            [weakSelf.credentialStore setFirstName:firstName];
            [weakSelf.credentialStore setLastName:lastName];
            [weakSelf.credentialStore setLevelOfStudy:levelOfStudy];

            [weakSelf.credentialStore setEmail:self.emailField.text];
            [weakSelf.credentialStore setPassword:self.passwordField.text];

            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            [manager.requestSerializer setValue:token forHTTPHeaderField:@"auth_token"];
            [manager setResponseSerializer:[CustomerResponseSerializer serializer]];
            NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kCustomerURL];

            [manager GET:urlString parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
                // progress implementation
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSDictionary *sources = [responseObject objectForKey:@"sources"];
                NSArray *cards = (NSArray *)sources[@"data"];
                [weakSelf.credentialStore setHasCreditCard:(cards.count > 0)];
                [weakSelf.credentialStore setSignUp:false];
                [SVProgressHUD dismiss];
                [self.navigationController popViewControllerAnimated:NO];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                [SVProgressHUD showErrorWithStatus:@"Failed to refresh cards."];

            }];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [SVProgressHUD showErrorWithStatus:@"Login Failed"];

        }];
    }
}

As you can see on success I store the items to the credential store but this currently a luck game where it works 40% of the time and doesn't 60% which is what's confusing. I'm also struggling to recreate the issue on the simulator.

If anyone has any ideas? or needs to see more code let me know.

Upvotes: 0

Views: 371

Answers (2)

Toland Hon
Toland Hon

Reputation: 4629

I had a similar issue and it was fixed when I added Keychain Sharing entitlements.

For more details on how to enable Keychain Sharing: https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW15

Upvotes: 1

Ketan Parmar
Ketan Parmar

Reputation: 27448

I think you forget to resume your network call. try like below if it works,

 - (void)login:(id)sender {

[SVProgressHUD show];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setResponseSerializer:[LoginResponseSerializer serializer]];

__weak typeof(self)weakSelf = self;

NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kLoginURL];

if (self.passwordField.text && self.emailField.text) {
    NSDictionary *params = @{ @"email": self.emailField.text, @"password": self.passwordField.text};

    self.sessionManager = manager;

    [[manager POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        // prgress implementation
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *user = [responseObject objectForKey:@"user"];


        NSString *token = user[@"auth_token"];
        NSString *avatarURL = user[@"avatar_url"];
        NSString *userID = [NSString stringWithFormat: @"%@", user[@"id"]];
        NSString *firstName = user[@"first_name"];
        NSString *lastName = user[@"last_name"];
        NSString *levelOfStudy = user[@"level"];

        BOOL teecher = [user[@"teecher"] boolValue];

        [weakSelf.credentialStore setTeecher:teecher];
        [weakSelf.credentialStore setUserId:userID];
        [weakSelf.credentialStore setAvatarURL:avatarURL];
        [weakSelf.credentialStore setAuthToken:token];
        [weakSelf.credentialStore setFirstName:firstName];
        [weakSelf.credentialStore setLastName:lastName];
        [weakSelf.credentialStore setLevelOfStudy:levelOfStudy];

        [weakSelf.credentialStore setEmail:self.emailField.text];
        [weakSelf.credentialStore setPassword:self.passwordField.text];

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager.requestSerializer setValue:token forHTTPHeaderField:@"auth_token"];
        [manager setResponseSerializer:[CustomerResponseSerializer serializer]];
        NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kCustomerURL];

        [[manager GET:urlString parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            // progress implementation
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *sources = [responseObject objectForKey:@"sources"];
            NSArray *cards = (NSArray *)sources[@"data"];
            [weakSelf.credentialStore setHasCreditCard:(cards.count > 0)];
            [weakSelf.credentialStore setSignUp:false];
            [SVProgressHUD dismiss];
            [self.navigationController popViewControllerAnimated:NO];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [SVProgressHUD showErrorWithStatus:@"Failed to refresh cards."];

        }] resume];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"Login Failed"];

    }] resume];
}



}

Upvotes: 0

Related Questions