I want to implement local authentication security in my iOS app but I'm getting an error and not able to figure out why I'm getting this.
I'm using iPhone 5s. Is that matters?
Code:
import UIKit
import LocalAuthentication
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func action(_ sender: Any) {
authenticateUser()
}
func authenticateUser() {
let authContext : LAContext = LAContext()
var error: NSError?
if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error){
authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric Check for application", reply: {(successful: Bool, error: NSError?) -> Void in
if successful{
print("TouchID Yes")
}
else{
print("TouchID No")
}
} as! (Bool, Error?) -> Void)
}
else{
authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: "Enter your Passcode", reply: {
(successful: Bool, error: NSError?) in
if successful{
print("PassCode Yes")
}
else{
print("PassCode No")
}
} as! (Bool, Error?) -> Void)
}
}
}
Error:
Thanks in advance.
Touch ID on iPhone X does not exist. Although Touch ID, as a feature, is only a few years old, it has already been scrapped on the latest version of the iPhone. You still have Touch ID on iPhone 8/8 Plus but not on iPhone X. That's the bad news.
Tap the icon for your account or collection at the top right and choose Settings > Security. If you're using a tablet, tap your account or collection at the top of the sidebar. Tap to turn on biometric unlock, then place your finger on the fingerprint sensor, or let your device scan your face or eyes.
Open the Settings app on your iPhone or iPad. Scroll down to the Touch ID & Passcode option. Enable the iTunes & App Store toggle. You might need to sign into your iTunes or App Store account to do this.
This code without typecasting should work
func authenticateUser() {
let authContext : LAContext = LAContext()
var error: NSError?
if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error){
authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric Check for application", reply: {successful, error -> Void in
if successful{
print("TouchID Yes")
}
else{
print("TouchID No")
}
}
)
}
else{
authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: "Enter your Passcode", reply: {
successful,error in
if successful{
print("PassCode Yes")
}
else{
print("PassCode No")
}
}
)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With