How to use Touch ID sensor in iOS 8
More complete snippet, swift style:
func authenticateUser() {
// Get the local authentication context.
let context = LAContext()
// Declare a NSError variable.
var error: NSError?
// Set the reason string that will appear on the authentication alert.
var reasonString = "Authentication is needed to access your notes."
// Check if the device can evaluate the policy.
if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
[context .evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in
if success {
}
else{
// If authentication failed then show a message to the console with a short description.
// In case that the error is a user fallback, then show the password alert view.
println(evalPolicyError?.localizedDescription)
switch evalPolicyError!.code {
case LAError.SystemCancel.toRaw():
println("Authentication was cancelled by the system")
case LAError.UserCancel.toRaw():
println("Authentication was cancelled by the user")
case LAError.UserFallback.toRaw():
println("User selected to enter custom password")
self.showPasswordAlert()
default:
println("Authentication failed")
self.showPasswordAlert()
}
}
})]
}
else{
// If the security policy cannot be evaluated then show a short message depending on the error.
switch error!.code{
case LAError.TouchIDNotEnrolled.toRaw():
println("TouchID is not enrolled")
case LAError.PasscodeNotSet.toRaw():
println("A passcode has not been set")
default:
// The LAError.TouchIDNotAvailable case.
println("TouchID not available")
}
// Optionally the error description can be displayed on the console.
println(error?.localizedDescription)
// Show the custom alert view to allow users to enter the password.
self.showPasswordAlert()
}
}
Source
The Local Authentication framework provides facilities for requesting authentication from users using Touch ID, following code snipped shows how you should request for authentication.
Objective C
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myReasonString = @"String explaining why app needs authentication";
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myReasonString
reply:^(BOOL succes, NSError *error) {
if (success) {
// User authenticated successfully
} else {
// Authenticate failed
}
}];
} else {
// Could not evaluate policy; check authError
}
Swift
let myContext = LAContext()
var authError: NSError?
// Set the reason string that will appear on the authentication alert.
var myReasonString = "String explaining why app needs authentication"
if myContext.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) {
[myContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: myReasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in
if success {
// User authenticated successfully
} else {
// Authenticate failed
}
})]
} else{
// Could not evaluate policy; check authError
}
You're looking for LocalAuthentication framework (login may be required to see).
Basically you're interested in LAContext class and its
canEvaluatePolicy:error:
and
evaluatePolicy:localizedReason:reply:
methods.
The canEvaluatePolicy:error:
method is used to check if TouchID authentication is available for you to use.
And use evaluatePolicy:localizedReason:reply:
to perform actual authentication check