菜鸟教程小白 发表于 2022-12-11 18:59:56

ios - 是否可以仅使用密码进行身份验证,即使设备在 ios 中具有 touch id 功能,swift


                                            <p><p>我想<strong>仅</strong>使用 <code>PassCode</code> 进行身份验证,即使设备具有 <code>Touch ID</code> 功能。我正在使用 <code>.deviceOwnerAuthentication</code> 评估策略方法。当我使用它时,</p>

<blockquote>
<ul>
<li>If user has enrolled touch id --&gt; alway ask for touch id</li>
<li>If use hasn&#39;t enrolled touch id --&gt; then only ask for passcode</li>
</ul>
</blockquote>

<p>我想要的是,即使用户已经注册了 <code>touch id</code>,只需要 <code>passcode</code>。希望您对此有所帮助。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这可以通过将 <code>SecAccessControlCreateFlags</code> 设置为 <code>.devicePasscode</code> 使用安全框架使用 <code>SecAccessControl</code> 来完成。</p>

<p>添加钥匙串(keychain)项,</p>

<pre><code>let secAccessControlbject: SecAccessControl = SecAccessControlCreateWithFlags(kCFAllocatorDefault,
                                                                      kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
                                                                      .devicePasscode,
                                                                      nil)!
    let dataToStore = &#34;AnyData&#34;.data(using: .utf8)!


    let insertQuery: NSDictionary = [
      kSecClass: kSecClassGenericPassword,
      kSecAttrAccessControl: secAccessControlbject,
      kSecAttrService: &#34;PasscodeAuthentication&#34;,
      kSecValueData: dataToStore as Any,
    ]

    let insertStatus = SecItemAdd(insertQuery as CFDictionary, nil)
</code></pre>

<p>要通过密码屏幕进行身份验证,请访问已保存的项目,</p>

<pre><code> let query: NSDictionary = [
      kSecClass:kSecClassGenericPassword,
      kSecAttrService: &#34;PasscodeAuthentication&#34;,
      kSecUseOperationPrompt : &#34;Sign in&#34;
    ]

    var typeRef : CFTypeRef?

    let status: OSStatus = SecItemCopyMatching(query, &amp;typeRef) //This will prompt the passcode.

    if (status == errSecSuccess)
    {
       print(&#34;Authentication Success&#34;)
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 是否可以仅使用密码进行身份验证,即使设备在 ios 中具有 touch id 功能,swift,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46723662/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46723662/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 是否可以仅使用密码进行身份验证,即使设备在 ios 中具有 touch id 功能,swift