我一直在使用 KeychainItemWrapper 就好了。但由于我已将手机更新到 iOS 9,因此由于某种原因它不会存储 sessionID。
+ (BOOL)createKeychainValue:(NSString *)value forIdentifier:(NSString *)identifier
{
NSMutableDictionary *dictionary = [self setupSearchDirectoryForIdentifier:identifier];
NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
[dictionary setObject:valueData forKey:(__bridge id)kSecValueData];
// Protect the keychain entry so it's only valid when the device is unlocked at least once.
[dictionary setObject:(__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible];
// **THIS LINE OF CODE RETURNS -34108**
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)dictionary, NULL);
// If the addition was successful, return. Otherwise, attempt to update existing key or quit (return NO).
if (status == errSecSuccess) {
return YES;
} else if (status == errSecDuplicateItem){
return [self updateKeychainValue:value forIdentifier:identifier];
} else {
return NO; **The call returns here...**
}
}
有人知道发生了什么吗?
编辑
最奇怪的事情:它只是不时发生并且总是处于 Debug模式。
编辑2
由于这只发生在 Debug模式下,我通常会根据变量的类型采取两种解决方法:
- 始终保持从本地钥匙串(keychain)加载的最后一个有效变量(例如 sessionID),并在 Debug模式下将其用作备份
- 如果可能,在调试时忽略无效值(在这种情况下,我会添加一个额外的控制变量来允许/禁止这些无效值)
(使用#ifdef DEBUG 检查您是否处于 Debug模式)
最佳答案
根据 Apple 的说法,目前没有解决方案。
https://forums.developer.apple.com/thread/4743
关于ios - KeychainItemWrapper 未在 iOS 9 OSStatus -34018 上加载(errSecMissingEntitlement),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32366157/