繁体   English   中英

如何在钥匙串iOS中存储NSNumber

[英]How to store NSNumber in Keychain iOS

我在iOS应用程序中使用了Rails api,并且在创建用户时,我将用户的响应存储在钥匙串中。 诸如用户auth_token,email等之类的teecher 。响应中的json字段之一是一个称为teecher的布尔值。

我正在努力弄清如何存储和撤回钥匙串。 我已经为凭证存储附加了现有代码,该代码当前正在保存详细信息。

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 这是我努力建立的地方

#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

最后,这里是我从响应中获取json并将其发送到凭证存储区时的代码。 不用担心,我没有在实例化凭证存储和设置变量的地方进行操作。

[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);
        }];

尝试像这样将NSNumber转换为NSString

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

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

暂无
暂无

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

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