菜鸟教程小白 发表于 2022-12-11 20:28:56

ios - 如何在不调用生物识别技术的情况下检查现有的钥匙串(keychain)项目


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

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

<pre><code>const SecAccessControlRef accessControl = SecAccessControlCreateWithFlags(nil, kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, kSecAccessControlUserPresence, &amp;accessControlError);
LAContext * const localAuthContext = [ 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);
</code></pre>

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

<pre><code>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);
</code></pre>

<p>有什么方法可以在不调用生物识别访问的情况下做到这一点?如果没有,我如何可靠地检查是否要添加或更新钥匙串(keychain)项?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我正在使用类似下面的东西...</p>
<pre><code>    func has(service: String, account: String, options: = []) -&gt; Bool {
      var query = self.query(service: service, account: account, options: options)
      query = internalContext

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

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

    private var internalContext: LAContext? = {
      let context = LAContext()
      context.interactionNotAllowed = true
      return context
    }()
</code></pre>
<p>以前您也可以使用 UseAuthenticationUIFail,但自 iOS 14 起已弃用。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在不调用生物识别技术的情况下检查现有的钥匙串(keychain)项目,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51942870/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51942870/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在不调用生物识别技术的情况下检查现有的钥匙串(keychain)项目