我正在尝试使用Apple的示例代码KeychainTouchID
中的钥匙串标准代码以下是我的代码片段
SecAccessControlRef sacObject = SecAccessControlCreateWithFlags(kCFAllocatorDefault,
kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
kSecAccessControlUserPresence, &error);
if (sacObject == NULL || error != NULL) {
NSString *errorString = [NSString stringWithFormat:@"SecItemAdd can't create sacObject: %@", error];
self.textView.text = [self.textView.text stringByAppendingString:errorString];
return;
}
// we want the operation to fail if there is an item which needs authentication so we will use
// kSecUseNoAuthenticationUI
NSDictionary *attributes = @{
(id)kSecClass: (id)kSecClassGenericPassword,
(id)kSecAttrService: @"SampleService",
(id)kSecValueData: [@"SECRET_PASSWORD_TEXT" dataUsingEncoding:NSUTF8StringEncoding],
(id)kSecUseAuthenticationUI: (id)kSecUseAuthenticationUIAllow,
(id)kSecAttrAccessControl: (__bridge_transfer id)sacObject
};
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)attributes, nil);
NSString *errorString = [self keychainErrorToString:status];
NSString *message = [NSString stringWithFormat:@"SecItemAdd status: %@", errorString];
[self printMessage:message inTextView:self.textView];
});
但是密钥链总是返回错误errSecAuthFailed
。相同的代码是代码正在一台iPhone6 +设备上运行,但它不适用于带有iOS 9.x的iPhone5。
经常尝试寻找原因,但没有找到任何有用的东西。我注意到的一件重要事情是,如果我没有设置归因kSecAttrAccessControl
,那么它在iPhone5上也能正常工作。所以,我认为这与设置访问控制属性有关。
任何帮助将不胜感激,