You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adding basic Touch ID authentication to a note taking application
Glossing over a lot of details for brevity, want to give the basic flavor and see Swift in action
Talk about TouchID
Introduced in iOS 7 for the iPhone 5s
Laser cut sapphire crystal to prevent scratching (wouldn't work)
Stainless steel detection ring to detect finger without pressing
New framework added for developers with iOS 8
Not the most secure thing in the world, but at least stored locally
Demo the "Secret Notes" app before adding the TouchID authentication
Mention using Reflector app because TouchID is not supported in the simulator
Add a note
Add a another note!
Delete a note
Le Code
MainViewController.swift
Import Local Authentication framework
import LocalAuthentication
Create a new function, authenticateUser()
Show what error cases we need to cover by opening up
LocalAuthentication.framework/Headers/LAError.h
Add code to authenticateUser()
funcauthenticateUser() {
// Get the local authentication contextletcontext:LAContext=LAContext()
// Declare an NSError variablevarerror:NSError? =nil// Set the reason string that will appear on the authentication alertletreasonString="Authentication required to view these top secret notes!"// Check if the device can evaluate the policyifcontext.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: {(success:Bool, evalPolicyError:NSError?)->Voidinifsuccess {
} else {
// If authentication failed then show a message to the console with a short description.switchevalPolicyError!.code {
caseLAError.SystemCancel.rawValue:
println("Authentication was cancelled by the system.")
caseLAError.UserCancel.rawValue:
println("Authentication was cancelled by the user.")
self.notes= [NSManagedObject]()
caseLAError.UserFallback.rawValue:
println("User selected to enter a custom password.")
default:
println("Could not authenticate.")
}
}
})
} else {
// The security policy can not be evaluated at all, so display a short message detailing whyswitcherror!.code {
caseLAError.TouchIDNotEnrolled.rawValue:
println("TouchID is not enrolled.")
caseLAError.PasscodeNotSet.rawValue:
println("Passcode is not set")
default:
println("Catastrophic error.")
}
}
}
Add a call to this function to the main view controller's viewDidAppear method
Demo the functionality so far on the phone
Hiding the Table View
First thing we can do is hide the tableView if the user taps "Cancel" on the Touch ID UIAlertView
caseLAError.UserCancel.rawValue:
println("Authentication was cancelled by the user.")
dispatch_async(dispatch_get_main_queue()) {
self.tableView.hidden= true
}
Make sure the table is visible when we successfully authenticate
Refactor the evalPolicyError switch statement to use this new function
Adding the Password Fallback
Explain the need to a fallback option, in case of thumbs being severed or something else fancy like that
Add comments to show where our new function, showPasswordAlert() will be called from (In two branches of the evalPolicyError switch, and as the last statement in the canEvalPolicy else clause)