MASTG-KNOW-0056: Local Authentication Framework
The Local Authentication framework provides facilities for requesting a passphrase or Touch ID authentication from users. Developers can display and utilize an authentication prompt by utilizing the function evaluatePolicy
of the LAContext
class.
Two available policies define acceptable forms of authentication:
-
deviceOwnerAuthentication
(Swift) orLAPolicyDeviceOwnerAuthentication
(Objective-C): When available, the user is prompted to perform Touch ID authentication. If Touch ID is not activated, the device passcode is requested instead. If the device passcode is not enabled, policy evaluation fails. -
deviceOwnerAuthenticationWithBiometrics
(Swift) orLAPolicyDeviceOwnerAuthenticationWithBiometrics
(Objective-C): Authentication is restricted to biometrics where the user is prompted for Touch ID.
The evaluatePolicy
function returns a boolean value indicating whether the user has authenticated successfully.
The Apple Developer website offers code samples for both Swift and Objective-C. A typical implementation in Swift looks as follows.
let context = LAContext()
var error: NSError?
guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) else {
// Could not evaluate policy; look at error and present an appropriate message to user
}
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Please, pass authorization to enter this area") { success, evaluationError in
guard success else {
// User did not authenticate successfully, look at evaluationError and take appropriate action
}
// User authenticated successfully, take appropriate action
}