iphone - 使用 KeyChainItemWrapper 在 KeyChain 中存储 key

标签 iphone objective-c keychain xcode

我正在使用 Apple 示例代码提供的 KeyChainItemWrapper 类将身份验证 token 保存到钥匙串(keychain)。

KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier"JetTaxiApp_AuthToken" accessGroup:nil];  

但是当我尝试将值设置为钥匙串(keychain)时,出现了一个奇怪的异常

[_authenticationTokenKeychain setObject:authenticationToken forKey: @"auth_token"];

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.'

钥匙串(keychain)还不存在(在调用时) 什么会导致此异常?

最佳答案

您需要使用标准 key ,所以这里您的 @"auth_token" 不正确。

The keys that can be used for this purpose and the possible values for each key are listed in the “Keychain Services Constants” section.

来源,包含有效常量列表:Keychain Services Reference

例如,您可以使用:

[_authenticationTokenKeychain setObject:authenticationToken forKey: (__bridge NSString *)kSecValueData];

关于iphone - 使用 KeyChainItemWrapper 在 KeyChain 中存储 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7117885/

相关文章:

iphone - ARC 释放子类 NSObject,但不释放自定义 NSObject

iphone - block 中对 self 的弱引用有时会导致 BAD_EXCESS

objective-c - Objective C 奇怪的十进制到 long long 的转换

objective-c - 从objective-c中的 "JSON-style"NSString获取正确的值

regex - 为什么 grep 不从安全实用程序的输出中提取?从 echo 作品中进行比较提取

iphone - 自定义 uitableviewcell 的问题

iphone - 检查数据是否存在于 NSMutableArray 中的索引处

objective-c - iPhone:使用 Game Kit 发送大数据

ios - 有没有办法获取SecKey的 key 类型?

ios - 如何从 Swift 中的 SecItemCopyMatching 返回多个匹配项