2
我發現有時在嘗試獲取之前通過使用Keychain
API成功存儲的安全項時遇到此錯誤。我在設備上運行的應用程序處於後臺狀態並鎖定屏幕時發現它。該設備還沒有鎖定代碼集,這是我打電話的功能:訪問Keychain中的安全項有時會在iOS中返回錯誤-25308(errSecInteractionNotAllowed)
+ (NSString *)findValueForKey:(NSString *)keyStr
{
NSString *valueStr = @"";
if ((keyStr != nil) && (![keyStr isEqualToString:@""])) {
NSString *service = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary *query = @{(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService : service,
(__bridge id)kSecAttrAccount : keyStr,
(__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue};
CFDataRef cfValue = NULL;
OSStatus results = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&cfValue);
if ([self checkIfNoError:results]) {
valueStr = [[NSString alloc] initWithData:(__bridge_transfer NSData *)cfValue encoding:NSUTF8StringEncoding];
}
else {
NSLog(@"%@", [self getErrorMessageForStatus:results]);
}
}
return valueStr;
}
和項目儲存通過調用這個方法:
+ (BOOL)storeInKeychainWithKey:(NSString *)keyStr withValueStr:(NSString *)valueStr
{
if ((keyStr != nil) && (![keyStr isEqualToString:@""]) &&
(valueStr != nil) && (![valueStr isEqualToString:@""])) {
NSData *valueData = [valueStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *service = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary *secItem = @{(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService : service,
(__bridge id)kSecAttrAccount : keyStr,
(__bridge id)kSecValueData : valueData};
CFTypeRef result = NULL;
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)secItem, &result);
NSLog(@"%@", [self getErrorMessageForStatus:status]);
return [self checkIfItem:status];
}
else {
return NO;
}
}
我認爲鑰匙串項目總是在iOS的訪問......這post似乎是關於類似的東西,但我不知道這是否是已被廢棄,我應該怎麼解決這個...提前
感謝