iOS KeychainItemWrapper不更新
我刚刚发现我的应用程序有一个有趣的问题。 在应用程序中,我将用户的用户名和密码保存到钥匙串中。
keychainWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyLoginPassword" accessGroup:nil]; [keychainWrapper setObject:usernameField.text forKey:(id)kSecAttrAccount]; [keychainWrapper setObject:passwordField.text forKey:(id)kSecValueData];
当这个代码在Debug中运行时,它似乎工作得很好。 它每次更新,我以后可以从钥匙链中检索项目。 当它在发行版中运行时,钥匙串永远不会被更新。 我已经validation,是这两行代码都在两个版本中。 我正在使用Xcode 4.2和iOS5 SDK,并在装有iOS5的iPad 2上运行应用程序。
我也遇到了这个问题,并且花了我一个时间才弄清楚
有一个“KeychainWrapper”的版本,在NSAssert(除其他之外)中有SecItemUpdate。
谁做这个是一个白痴!当build立释放/分配每个NSAssert是无效的,这意味着代码甚至不能运行。
例如:
NSAssert(SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)tempCheck), @"Couldn't update the Keychain Item." );
需要成为
OSStatus status = SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)tempCheck); NSAssert(status == noErr, @"Couldn't update the Keychain Item." );
注意实际的SecItemUpdate是如何移出NSAssert的,而是检查结果
重要说明:尝试更新kSecValueData的值,而不指定kSecAttrAccount的值,也会导致断言失败。 因此,如果您的意图是存储一串敏感数据(如信用卡号码列表),请务必在kSecAttrAccount属性中存储一些“帐户名称”文本,如下所示:
static NSString* kCardListXML = @"cardListXML"; static NSString* cardListAccountName = @"cardListAccount"; -(void)setCardListXML:(NSString*)xml { KeychainItemWrapper* wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:kCardListXML accessGroup:nil]; [wrapper setObject:cardListAccountName forKey:(id)CFBridgingRelease(kSecAttrAccount)]; [wrapper setObject:xml forKey:(id)CFBridgingRelease(kSecValueData)]; } -(NSString*)getCardListXML { KeychainItemWrapper* wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:kCardListXML accessGroup:nil]; [wrapper setObject:cardListAccountName forKey:(id)CFBridgingRelease(kSecAttrAccount)]; return [wrapper objectForKey:CFBridgingRelease(kSecValueData)]; }
当你包括
keychainWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyLoginPassword" accessGroup:nil]; [keychainWrapper setObject:usernameField.text forKey:(id)kSecAttrAccount]; [keychainWrapper setObject:passwordField.text forKey:(id)kSecValueData];
你也必须包括
[keychainWrapper setObject:@"Myappstring" forKey: (id)kSecAttrService];
或者我得到一个“SIGABRT”错误。 (Myappstring)是一个定义你的应用程序的string。
也许我错过了一些东西,至less应该这样做。