[英]iOS KeychainItemWrapper not updating
我剛發現我的應用程序有一個有趣的問題。 在應用程序中,我將用戶的用戶名和密碼保存到鑰匙串。
keychainWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyLoginPassword" accessGroup:nil];
[keychainWrapper setObject:usernameField.text forKey:(id)kSecAttrAccount];
[keychainWrapper setObject:passwordField.text forKey:(id)kSecValueData];
當這個代碼在Debug中運行時,似乎工作正常。 它每次都會更新,之后我可以從鑰匙串中檢索這些項目。 當它在Distribution中運行時,鑰匙串永遠不會更新。 我已經驗證了這兩行代碼都會遇到這些代碼行。 我正在使用Xcode 4.2和iOS5 SDK,並在安裝了iOS5的iPad 2上運行應用程序。
我也有這個問題,我花了很長時間才弄明白
有一個版本的“KeychainWrapper”浮動,它有一個NSAssert內的SecItemUpdate(以及其他東西)。
無論誰做了這個都是白痴!當構建發布/分發時,每個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)是一個定義應用程序的字符串。
也許我錯過了一些東西,至少應該做一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.