iOS8 check if device has Touch ID

后端 未结 5 632
不思量自难忘°
不思量自难忘° 2021-02-12 23:50

LAContext has method to check if device can evaluate touch ID and gives error message. Problem is that same error message \"LAErrorPasscodeNotSet\" is given by system in two cas

5条回答
  •  你的背包
    2021-02-13 00:13

    Here you can check, Touch-ID and Face-ID both (with iOS 11+)

    Use property biometryType of LAContext to check and evaluate available biometric policy. (For a passcode authentication , when biometric fails, use: LAPolicyDeviceOwnerAuthentication)

    Try this and see:

    Objective-C:

    LAContext *laContext = [[LAContext alloc] init];
    
    NSError *error;
    
    
    // For a passcode authentication , when biometric fails, use: LAPolicyDeviceOwnerAuthentication
    //if ([laContext canEvaluatePolicy: LAPolicyDeviceOwnerAuthentication error:&error]) {
    if ([laContext canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {    
        if (error != NULL) {
            // handle error
        } else {
    
            if (@available(iOS 11, *)) {
                if (laContext.biometryType == LABiometryTypeFaceID) {
                    //localizedReason = "Unlock using Face ID"
                    NSLog(@"FaceId support");
                } else if (laContext.biometryType == LABiometryTypeTouchID) {
                    //localizedReason = "Unlock using Touch ID"
                    NSLog(@"TouchId support");
                } else {
                    //localizedReason = "Unlock using Application Passcode"
                    NSLog(@"No biometric support or Denied biometric support");
                }
            } else {
                // Fallback on earlier versions
            }
    
    
            [laContext evaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Test Reason" reply:^(BOOL success, NSError * _Nullable error) {
    
                if (error != NULL) {
                    // handle error
                } else if (success) {
                    // handle success response
                } else {
                    // handle false response
                }
            }];
        }
    }
    

    Swift:

    let laContext = LAContext()
    var error: NSError?
    let biometricsPolicy = LAPolicy.deviceOwnerAuthentication //LAPolicy.deviceOwnerAuthenticationWithBiometrics
    
    if laContext.isCredentialSet(LACredentialType.applicationPassword) {
        print("Passsword is set")
    }
    
    let localizedFallbackTitle = "Unlock Using Device Passcode"
    let localizedCancelTitle = "Use Application Passcode"
    if (laContext.canEvaluatePolicy(biometricsPolicy, error: &error)) {
    
        if let laError = error {
            print("laError - \(laError)")
            return
        }
    
    
    
        //print("biometricsPolicy - \(biometricsPolicy.rawValue)")
    
        UINavigationBar.appearance().tintColor = UIColor.red
    
    
        var localizedReason = "My Reason to be displayed on face id prompt"
        if #available(iOS 11.0, *) {
            if (laContext.biometryType == LABiometryType.faceID) {
                //localizedReason = "Unlock using Face ID"
                print("FaceId support")
            } else if (laContext.biometryType == LABiometryType.touchID) {
                //localizedReason = "Unlock using Touch ID"
                print("TouchId support")
            } else {
                //localizedReason = "Unlock using Application Passcode"
                print("No Biometric support")
            }
        } else {
            // Fallback on earlier versions
        }
    
        laContext.localizedFallbackTitle = localizedFallbackTitle
        laContext.localizedCancelTitle = localizedCancelTitle
        //laContext.localizedReason = "test loc reason"
        laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in
    
            DispatchQueue.main.async(execute: {
    
                if let laError = error {
                    print("laError - \(laError)")
                } else {
                    if isSuccess {
                        print("sucess")
                    } else {
                        print("failure")
                    }
                }
    
            })
        })
    }
    

提交回复
热议问题