Determine if an iOS device supports TouchID without setting passcode

前端 未结 7 1717
一生所求
一生所求 2020-12-25 13:27

I\'m currently developing an iOS app that enables users to log in to the app using TouchID, but firstly they must set up a password inside the app first. Problem is, to show

7条回答
  •  有刺的猬
    2020-12-25 14:03

    I know this is a question from last year, but this solution does not make what you need? (Swift code)

    if #available(iOS 8.0, *) {
        var error: NSError?
        let hasTouchID = LAContext().canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error)
    
        //Show the touch id option if the device has touch id hardware feature (even if the passcode is not set or touch id is not enrolled)
        if(hasTouchID || (error?.code != LAError.TouchIDNotAvailable.rawValue)) {
            touchIDContentView.hidden = false
        } 
    }
    

    Then, when the user presses the button to log in with touch id:

    @IBAction func loginWithTouchId() {
        let context = LAContext()
    
        var error: NSError?
        let reasonString = "Log in with Touch ID"
    
        if (context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error)) {
            [context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in
                //Has touch id. Treat the success boolean
            })]
        } else { 
            //Then, if the user has touch id but is not enrolled or the passcode is not set, show a alert message
            switch error!.code{
    
            case LAError.TouchIDNotEnrolled.rawValue:
                //Show alert message to inform that touch id is not enrolled
                break
    
            case LAError.PasscodeNotSet.rawValue:
                //Show alert message to inform that passcode is not set
                break
    
            default:
                // The LAError.TouchIDNotAvailable case.
                // Will not catch here, because if not available, the option will not visible
            }
        }
    }
    

    Hope it helps!

提交回复
热议问题