如何在钥匙串中存储 NSNumber iOS
How to store NSNumber in Keychain iOS
我在我的 iOS 应用程序中使用 rails api,当我创建用户时,我将用户的响应存储在钥匙串中。诸如用户 auth_token、电子邮件等之类的东西。响应中的 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];
}
我在我的 iOS 应用程序中使用 rails api,当我创建用户时,我将用户的响应存储在钥匙串中。诸如用户 auth_token、电子邮件等之类的东西。响应中的 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];
}