Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the return value type of canAuthenticate() #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,24 @@ open class BioMetricAuthenticator: NSObject {
public extension BioMetricAuthenticator {

/// checks if biometric authentication can be performed currently on the device.
class func canAuthenticate() -> Bool {
class func canAuthenticate() -> CheckAuthenticateAbleResult<AuthenticationError> {

var isBiometricAuthenticationAvailable = false
var error: NSError? = nil

if LAContext().canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
isBiometricAuthenticationAvailable = (error == nil)
}
return isBiometricAuthenticationAvailable

if isBiometricAuthenticationAvailable,
error == nil {
return CheckAuthenticateAbleResult.success
} else if let err = error{
let authenticationError = AuthenticationError.initWithError(err as! LAError)
return CheckAuthenticateAbleResult.failure(authenticationError)
} else {
return CheckAuthenticateAbleResult.failure(nil)
}
}

/// Check for biometric authentication
Expand All @@ -87,8 +96,8 @@ public extension BioMetricAuthenticator {
}

// authenticate
BioMetricAuthenticator.shared.evaluate(policy: LAPolicy.deviceOwnerAuthenticationWithBiometrics, with: context, reason: reasonString, success: successBlock, failure: failureBlock)
}
BioMetricAuthenticator.shared.evaluate(policy: LAPolicy.deviceOwnerAuthenticationWithBiometrics, with: context, reason: reasonString, success: successBlock, failure: failureBlock)
}

/// Check for device passcode authentication
class func authenticateWithPasscode(reason: String, cancelTitle: String? = "", success successBlock:@escaping AuthenticationSuccess, failure failureBlock:@escaping AuthenticationFailure) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// CheckAuthenticateAbleResult.swift
// BiometricAuthentication
//
// Created by linjj on 2018/12/13.
// Copyright © 2018 Rushi Sangani. All rights reserved.
//

import Foundation

public enum CheckAuthenticateAbleResult<T> {
case success
case failure(T?)
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ class ViewController: UIViewController {
// Set AllowableReuseDuration in seconds to bypass the authentication when user has just unlocked the device with biometric
BioMetricAuthenticator.shared.allowableReuseDuration = 60

// check biometric authentication usable
let url = BioMetricAuthenticator.canAuthenticate()
switch url {

case .failure(let error):
if error == .biometryNotAvailable {
showErrorAlert(message: "Biometry is not available on the device or disabled in settings.")
} else if error == .biometryNotEnrolled {
showErrorAlert(message: "The user has no enrolled biometric identities.")
} else if error == .biometryLockedout {
showErrorAlert(message: "Biometry is locked because there were too many failed attempts.")
} else {
showErrorAlert(message: "unkown error")
}
case .success:
startAuthentication()

}

}

func startAuthentication() {
// start authentication
BioMetricAuthenticator.authenticateWithBioMetrics(reason: "", success: {

Expand Down