I have implemented Class LocalAuthentication that provide facilities for requesting authentication. We can use it on only device having touch ID.I made it in singleton pattern to be convenient.We can even use it on Air2.
import LocalAuthentication class LocalAuthMgr: NSObject { let myContext = LAContext() let myLocalizedReasonString = "testrun" var authError: NSError? = nil static let shared:LocalAuthMgr = LocalAuthMgr() private override init() {} func auth() { if #available(iOS 8.0, OSX 10.12, *) { if myContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &authError) { myContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { (success, evaluateError) in if (success) { print("suc") } else { print("er1") } } } else { print("er2") } } else { print("er3") } } }
If you execute on Air2,you can get the following error:Code=-7 "No fingers are enrolled with Touch ID.".
If you cancel on iPhone7,you can get the following errpr:Code=-2 "Canceled by user.".
If you have fail three times,you can get the following error:Code=-1 "Application retry limit exceeded.". In the case,you will not be able to use Touch Id on the device unless you enter your password.
If you choice "input password",you can get the following error:Code=-3 "Fallback authentication mechanism selected.".
You can set the duration for which Touch ID authentication reuse is allowable.
LAContext class provides property of "touchIDAuthenticationAllowableReuseDuration". If you set the specified time interval to this property,the authentication for the receiver succeed automatically. The default value is 0 and the maximum value is 300( this is constant).