我对 iOS 开发非常陌生,所以如果这是一个新手问题,请原谅我。我的应用程序有一个简单的身份验证机制,它需要用户的电子邮件地址和密码。我还有一个开关,上面写着“记住我”。如果用户打开该开关,我想保留他们的电子邮件/密码,以便将来可以自动填充这些字段。
我已经将其与保存到 plist 文件一起使用,但我知道这不是最好的主意,因为密码未加密。我找到了一些保存到钥匙串的示例代码,但说实话,我有点迷失。对于下面的函数,我不确定如何调用它以及如何修改它以保存电子邮件地址。
我猜调用它会是:saveString(@"passwordgoeshere");
感谢您的任何帮助!!!
+ (void)saveString:(NSString *)inputString forKey:(NSString *)account {
NSAssert(account != nil, @"Invalid account");
NSAssert(inputString != nil, @"Invalid string");
NSMutableDictionary *query = [NSMutableDictionary dictionary];
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:account forKey:(id)kSecAttrAccount];
[query setObject:(id)kSecAttrAccessibleWhenUnlocked forKey:(id)kSecAttrAccessible];
OSStatus error = SecItemCopyMatching((CFDictionaryRef)query, NULL);
if (error == errSecSuccess) {
// do update
NSDictionary *attributesToUpdate = [NSDictionary dictionaryWithObject:[inputString dataUsingEncoding:NSUTF8StringEncoding]
forKey:(id)kSecValueData];
error = SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)attributesToUpdate);
NSAssert1(error == errSecSuccess, @"SecItemUpdate failed: %d", error);
} else if (error == errSecItemNotFound) {
// do add
[query setObject:[inputString dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecValueData];
error = SecItemAdd((CFDictionaryRef)query, NULL);
NSAssert1(error == errSecSuccess, @"SecItemAdd failed: %d", error);
} else {
NSAssert1(NO, @"SecItemCopyMatching failed: %d", error);
}
}
我编写了一个简单的包装器,允许将任何符合 NSCoding 的对象保存到钥匙串中。例如,您可以将电子邮件和密码存储在 NSDictionary 中,并使用此类将 NSDictionary 存储到钥匙串中。
简单钥匙串.h
#import <Foundation/Foundation.h>
@class SimpleKeychainUserPass;
@interface SimpleKeychain : NSObject
+ (void)save:(NSString *)service data:(id)data;
+ (id)load:(NSString *)service;
+ (void)delete:(NSString *)service;
@end
简单钥匙串.m
#import "SimpleKeychain.h"
@implementation SimpleKeychain
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassGenericPassword, (id)kSecClass,
service, (id)kSecAttrService,
service, (id)kSecAttrAccount,
(id)kSecAttrAccessibleAfterFirstUnlock, (id)kSecAttrAccessible,
nil];
}
+ (void)save:(NSString *)service data:(id)data {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}
+ (id)load:(NSString *)service {
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)keyData];
}
@catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", service, e);
}
@finally {}
}
if (keyData) CFRelease(keyData);
return ret;
}
+ (void)delete:(NSString *)service {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}
@end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)