iOS KeychainItemWrapper没有更新 [英] iOS KeychainItemWrapper not updating

查看:144
本文介绍了iOS KeychainItemWrapper没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚发现我的应用程序存在一个有趣的问题。在应用程序中,我将用户的用户名和密码保存到钥匙串。

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中运行时,钥匙串永远不会更新。我已经验证是这些代码行在两个版本中都被命中。我正在使用Xcode 4.2和iOS5 SDK并在安装了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(以及其他东西)中的SecItemUpdate。

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没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆