caldera.sac
caldera.sac

Reputation: 5088

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.

Upvotes: 3

Views: 1933

Answers (2)

Nandha
Nandha

Reputation: 6766

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")
    }

Upvotes: 3

iOS Developer
iOS Developer

Reputation: 1

@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

Upvotes: 0

Related Questions