Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 321
Chanaka Anuradh Caldera Avatar asked Nov 30 '25 16:11

Chanaka Anuradh Caldera


1 Answers

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")
    }
like image 90
Nandha Avatar answered Dec 02 '25 07:12

Nandha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!