問題描述
我剛剛發現我的應用程序存在一個有趣的問題.在應用程序中,我將用戶的用戶名和密碼保存到鑰匙串中.
I just found an interesting problem with my app. In the app I am saving the user's user name and password to the keychain.
keychainWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyLoginPassword" accessGroup:nil];
[keychainWrapper setObject:usernameField.text forKey:(id)kSecAttrAccount];
[keychainWrapper setObject:passwordField.text forKey:(id)kSecValueData];
當這段代碼在 Debug 中運行時,它似乎工作得很好.它每次都會更新,我以后可以從鑰匙串中檢索項目.當它在 Distribution 中運行時,鑰匙串永遠不會更新.我已經驗證是的,這些代碼行在兩個版本中都被命中.我正在使用帶有 iOS5 SDK 的 Xcode 4.2,并在安裝了 iOS5 的 iPad 2 上運行該應用程序.
When this code is run in Debug it seems to work just fine. It updates each time and I can later retrieve the items from the keychain. When it is run in Distribution however the keychain never gets updated. I have verified that yes these lines of code are hit in both builds. I am using Xcode 4.2 with the iOS5 SDK and running the app on an iPad 2 with iOS5 installed.
推薦答案
我也遇到了這個問題,想了很久
I also had this problem, and it took me forever to figure out
有一個版本的KeychainWrapper"在 NSAssert 中浮動(除其他外).
There is a version of "KeychainWrapper" floating around that has it's SecItemUpdate within an NSAssert (among other things).
做這件事的人是個白癡!在為發布/分發而構建時,每個 NSAssert 都無效,這意味著代碼甚至無法運行.
Whoever did this is a moron!, when building for release/distribution every NSAssert is nullified, meaning that code doesn't even get run.
例如:
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 之外的,而是檢查結果
Notice how the actual SecItemUpdate is moved outside the NSAssert, and instead the result is checked
重要提示:嘗試更新 kSecValueData 的值,而不指定 kSecAttrAccount 的值,也會導致斷言失敗.因此,如果您的意圖是存儲單個敏感數據字符串(例如信用卡號碼列表),請務必在 kSecAttrAccount 屬性中存儲一些帳戶名稱"文本,如下所示:
Important note: Attempting to update a value for kSecValueData, without also specifying a value for kSecAttrAccount, will cause the assertion to fail as well. So, if your intent is to store a single string of sensitive data (such as a list of credit card numbers), be sure to store some "account name" text in the kSecAttrAccount attribute, like so:
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)];
}
這篇關于iOS KeychainItemWrapper 未更新的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!