IOS 之Touch ID
let context = LAContext()
var error:NSError?
let avaliable = context.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error:&error )
if avaliable{
buttonUseTouchId.enabled = true
}else{
let alertController = UIAlertController(title: "Touch ID",
message: "Touch ID is not available",
preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK",
style: .Default,
handler: nil))
presentViewController(alertController, animated: true, completion: nil)
}
}
let context = LAContext()
let reason = "Please authenticate with Touch ID " +
"to access your private information"
context.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics,
localizedReason: reason, reply: {success, error in
if success{
/* The user was successfully authenticated */
print("The user was successfully authenticated ")
} else {
/* The user could not be authenticated */
print("The user could not be authenticated ")
}
})
import UIKit
import LocalAuthentication
class ViewController: UIViewController {
@IBOutlet weak var buttonCheckTouchId: UIButton!
@IBOutlet weak var buttonUseTouchId: UIButton!
@IBAction func checkTouchIdAvailability(sender: AnyObject) {
let context = LAContext()
var error:NSError?
let avaliable = context.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error:&error )
if avaliable{
buttonUseTouchId.enabled = true
}else{
let alertController = UIAlertController(title: "Touch ID",
message: "Touch ID is not available",
preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK",
style: .Default,
handler: nil))
presentViewController(alertController, animated: true, completion: nil)
}
}
@IBAction func useTouchId(sender: AnyObject) {
/* We will code this soon */
let context = LAContext()
let reason = "Please authenticate with Touch ID " +
"to access your private information"
context.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics,
localizedReason: reason, reply: {success, error in
if success{
/* The user was successfully authenticated */
print("The user was successfully authenticated ")
} else {
/* The user could not be authenticated */
print("The user could not be authenticated ")
}
})
}
}