To use the TouchID in your app, first include LocalAuthentication.framework and then add some code. The reason for implementing dispatch_async is that it somehow speeds up the UI updating process.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import UIKit
import LocalAuthentication
class ViewController: UIViewController {
@IBOutlet weak var statusLabel: UILabel!
@IBAction func authAction(sender: AnyObject) {
authAction()
}
override func viewDidLoad() {
super.viewDidLoad()
}
func authAction() {
let laContext = LAContext()
var authError : NSError?
let authReason = "Unlock the app"
if laContext.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) {
laContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: authReason, reply: { (success, error) -> Void in
if success {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.successForLogin()
})
} else {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.failedForLogin()
})
}
})
}
}
func successForLogin() {
statusLabel.text = "Succeeded"
}
func failedForLogin() {
statusLabel.text = "Failed"
}
}