TouchID verification in IOS Swift

TouchID verification

In IOS,Face ID and Touch ID are secure, familiar authentication methods that people trust.

1).LocalAuthentication

The first job is to import the LocalAuthentication to the class/Viewcontroller.

2). LAContext

Need to create an LAContext object , which will provide a UI for evaluating the authentication process.

3). Result Handling

Handling UI based on the result for further process.

Create Separate Class for TouchID Verification - TouchVerify.swift

//

// TouchVerify.swift

// VerifyApp

//

// Created by Betamonks on 21/11/18.

// Copyright © 2018 Betamonks. All rights reserved.

//

import Foundation

import LocalAuthentication

class TouchIDVerify

{

public static var message: String = ""

public static var result = false

public static func verify(onSuccess responseManager: @escaping (_ Result: Bool) -> Void) {

print("Touch ID Verification Function")

let myContext = LAContext()

let myLocalizedReasonString = "Biometric Authentication - Function"

var authError: NSError?

if #available(iOS 8.0, macOS 10.12.1, *) {

if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {

myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in

DispatchQueue.main.async {

if success {

// User authenticated successfully, take appropriate action

self.message = "Awesome!!... User authenticated successfully"

responseManager(true)

} else {

// User did not authenticate successfully, look at error and take appropriate action

self.message = "Sorry!!... User did not authenticate successfully"

responseManager(false)

}

}

}

} else {

// Could not evaluate policy; look at authError and present an appropriate message to user

self.message = "Sorry!!.. Could not evaluate policy."

responseManager(false)

}

} else {

// device is not supported

self.message = "Ooops!!.. This feature is not supported."

responseManager(false)

}

}

}

Code Usage:

TouchIDVerify.verify(onSuccess: { (Result) in

if(Result)

{

// Success Action

print(Result)

print(TouchIDVerify.message)

}else

{

// Failure Action

print(Result)

print(TouchIDVerify.message)

}

})


You can Call from Button Click @IBAction or viewDidLoad