简体   繁体   中英

Is it possible to authenticate using only the passcode, even device has touch id capability in ios, swift

I want to authenticate only using the PassCode even device has the Touch ID Feature. I'm using .deviceOwnerAuthentication evaluate policy method. when I use this,

  • If user has enrolled touch id --> alway ask for touch id
  • If use hasn't enrolled touch id --> then only ask for passcode

what I want is, even user has enrolled for the touch id , ask only for the passcode . hope your help with this.

This can be done using Security framework using SecAccessControl by setting SecAccessControlCreateFlags as .devicePasscode .

Add a keychain item,

let secAccessControlbject: SecAccessControl = SecAccessControlCreateWithFlags(kCFAllocatorDefault,
                                                                      kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
                                                                      .devicePasscode,
                                                                      nil)!
    let dataToStore = "AnyData".data(using: .utf8)!


    let insertQuery: NSDictionary = [
        kSecClass: kSecClassGenericPassword,
        kSecAttrAccessControl: secAccessControlbject,
        kSecAttrService: "PasscodeAuthentication",
        kSecValueData: dataToStore as Any,
    ]

    let insertStatus = SecItemAdd(insertQuery as CFDictionary, nil)

To authenticate with the Passcode screen access the saved item,

 let query: NSDictionary = [
        kSecClass:  kSecClassGenericPassword,
        kSecAttrService  : "PasscodeAuthentication",
        kSecUseOperationPrompt : "Sign in"
    ]

    var typeRef : CFTypeRef?

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

    if (status == errSecSuccess)
    {
       print("Authentication Success")
    }

@Anuradh, As we cannot use iOS's passcode within our app for authentication, unless we use the TouchID I don't think there is a way to prompt just the default passcode screen. It's private API. Your best option is to use a custom passcode screen. You can check this 'ABPadLockScreen': https://github.com/abury/ABPadLockScreen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM