OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在不调用生物识别技术的情况下检查现有的钥匙串(keychain)项目 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:28
标题: ios - 如何在不调用生物识别技术的情况下检查现有的钥匙串(keychain)项目

我有一个将用户密码存储在设备钥匙串(keychain)中的应用程序,并且可以使用设备生物识别技术(Face ID 或 Touch ID)访问它。

我通过以下方式成功地做到了这一点:

const SecAccessControlRef accessControl = SecAccessControlCreateWithFlags(nil, kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, kSecAccessControlUserPresence, &accessControlError);
LAContext * const localAuthContext = [[LAContext alloc] init];
NSDictionary * const addQuery = @{
   (__bridge NSString *)kSecClass: (__bridge NSString *)kSecClassGenericPassword,
   (__bridge NSString *)kSecAttrAccount: username,
   (__bridge NSString *)kSecAttrAccessControl: (__bridge id)accessControl,
   (__bridge NSString *)kSecUseAuthenticationContext: localAuthContext,
   (__bridge NSString *)kSecValueData: passwordData
};
const OSStatus addStatus = SecItemAdd((__bridge CFDictionaryRef)addQuery, nil);

当我想更新密码时出现问题。我需要使用 SecItemUpdate(...) 函数。因此,我实现了一项检查,以查看给定用户名是否已存在该项目,但由于该项目的存储方式,我的 iPhone X 上的 Face ID 提示出现了。

NSDictionary * const findQuery = @{
   (__bridge NSString *)kSecClass: (__bridge NSString *)kSecClassGenericPassword,
   (__bridge NSString *)kSecAttrAccount: username,
   (__bridge NSString *)kSecReturnData: @(NO)
};
const OSStatus readStatus = SecItemCopyMatching((__bridge CFDictionaryRef)findQuery, nil);

有什么方法可以在不调用生物识别访问的情况下做到这一点?如果没有,我如何可靠地检查是否要添加或更新钥匙串(keychain)项?



Best Answer-推荐答案


我正在使用类似下面的东西...

    func has(service: String, account: String, options: [Options] = []) -> Bool {
        var query = self.query(service: service, account: account, options: options)
        query[kSecUseAuthenticationContext as String] = internalContext

        var secItemResult: CFTypeRef?
        status = SecItemCopyMatching(query as CFDictionary, &secItemResult)

        return status == errSecSuccess || status == errSecInteractionNotAllowed
    }

    private var internalContext: LAContext? = {
        let context = LAContext()
        context.interactionNotAllowed = true
        return context
    }()

以前您也可以使用 UseAuthenticationUIFail,但自 iOS 14 起已弃用。

关于ios - 如何在不调用生物识别技术的情况下检查现有的钥匙串(keychain)项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51942870/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4