简体   繁体   中英

How to store NSNumber in Keychain iOS

I'm using a rails api with my iOS app and when I create a user I store the response for the user in the keychain. Things like the users auth_token, email etc. One of the json field in the response is a boolean called teecher .

I'm struggling to firgure out how I can store and retreive this from the keychain. I've attached my existing code for my credential store which is currently saving the details.

CredentialStore.h

#import <Foundation/Foundation.h>

@interface TeechCredentialStore : NSObject

- (BOOL) isLoggedIn;
- (void) clearSavedCredentials;
- (NSString *)authToken;
- (NSString *)firstName;
- (NSString *)lastName;
- (NSString *)email;
- (NSString *)password;
- (NSString *)avatarURL;
- (NSString *)userId;
- (NSNumber *)teecher;
- (void) setAuthToken: (NSString *)authToken;
- (void) setFirstName: (NSString *)firstName;
- (void) setLastName: (NSString *)lastName;
- (void) setEmail: (NSString *)email;
- (void) setPassword: (NSString *)password;
- (void) setAvatarURL: (NSString *)avatar;
- (void) setUserId: (NSString *)userId;
- (void) setIsTeecher: (NSNumber *)teecher;

@end

CredentialStore.m This is where I'm struggling to set up

#define SERVICE_NAME @"Teechy"
#define AUTH_TOKEN_KEY @"auth_token"
#define FIRST_NAME_KEY @"first_name"
#define LAST_NAME_KEY @"last_name"
#define EMAIL_KEY @"email"
#define PASSWORD_KEY @"password"
#define AVATAR_URL @"avatar_url"
#define USER_ID @"id"
#define TEECHER_KEY @"teecher"

@implementation TeechCredentialStore

- (BOOL)isLoggedIn {
    return [self authToken] != nil;
}

- (void)clearSavedCredentials {
    [self setAuthToken:nil];
}

- (NSString *)authToken {
    return [self secureValueForKey:AUTH_TOKEN_KEY];
}

- (NSString *)firstName {
    return [self secureValueForKey:FIRST_NAME_KEY];
}

- (NSString *)lastName {
    return [self secureValueForKey:LAST_NAME_KEY];
}

- (NSString *)email {
    return [self secureValueForKey:EMAIL_KEY];
}

- (NSString *)password {
    return [self secureValueForKey:PASSWORD_KEY];
}

- (NSString *)avatarURL {
    return [self secureValueForKey:AVATAR_URL];
}

- (NSString *)userId {
    return [self secureValueForKey:USER_ID];
}

- (NSNumber *)teecher {
    return [self secureValueForKey:TEECHER_KEY];
}

- (void)setAuthToken:(NSString *)authToken {
    [self setSecureValue:authToken forKey:AUTH_TOKEN_KEY];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"token-changed" object:self];
}

- (void) setFirstName:(NSString *)firstName  {
    [self setSecureValue:firstName forKey:FIRST_NAME_KEY];
}

- (void) setLastName:(NSString *)lastName {
    [self setSecureValue:lastName forKey:LAST_NAME_KEY];
}

- (void) setEmail: (NSString *)email {
    [self setSecureValue:email forKey:EMAIL_KEY];
}

- (void) setPassword: (NSString *)password {
    [self setSecureValue:password forKey:PASSWORD_KEY];
}

- (void) setAvatarURL:(NSString *)avatar {
    [self setSecureValue:avatar forKey:AVATAR_URL];
}

- (void) setUserId: (NSString *)userId {
    [self setSecureValue:userId forKey:USER_ID];
}

- (void) setTeecher:(NSNumber *)teecher {
    [self setSecureValue:teecher forKey:TEECHER_KEY];
}

- (void)setSecureValue:(NSString *)value forKey:(NSString *)key {
    if (value) {
        [SSKeychain setPassword:value
                     forService:SERVICE_NAME
                        account:key];
    } else {
        [SSKeychain deletePasswordForService:SERVICE_NAME account:key];
    }
}

- (NSString *)secureValueForKey:(NSString *)key {
    return [SSKeychain passwordForService:SERVICE_NAME account:key];
}

@end

Finally here is the code when I'm taking the json I get back from the response and sending it to credential store. Bare in mind I'm leaving out where I instantiate the credential store and setup variables.

[manager POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
         // prgress implementation
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *JSON = [responseObject objectAtIndex:0];
            NSDictionary *user = JSON[@"user"];
            NSString *token = user[@"auth_token"];
            NSString *avatarURL = user[@"avatar_url"];


            weakSelf.credentialStore.avatarURL = avatarURL;
            weakSelf.credentialStore.authToken = token;
            weakSelf.credentialStore.firstName = self.firstNameField.text;
            weakSelf.credentialStore.lastName = self.lastNameField.text;
            weakSelf.credentialStore.email = self.emailField.text;
            weakSelf.credentialStore.password = self.passwordField.text;


            [SVProgressHUD dismiss];
            [self dismissViewControllerAnimated:YES completion:nil];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            if (task.isCancelled) {
//                return;
//            }
            [SVProgressHUD showErrorWithStatus:@"Login Failed"];
            NSLog(@"%@", error);
        }];

Try converting NSNumber into NSString like this

- (void) setTeecher:(BOOL)teecher  {
    [self setSecureValue:[@(teecher) stringValue] forKey:TEECHER_KEY];
}

- (BOOL)teecher {
    return [[self secureValueForKey:TEECHER_KEY] boolValue];
}

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