top of page

Add IOS Biometrics (Face-ID/Touch-ID)|Swift -4

Writer's picture: Rakesh KumarRakesh Kumar

Authenticate with Touch-Id/Face-Id

If you want to add touch/face id security to any of your feature in the iOS app then here you go


 

import Foundation

import UIKit

import LocalAuthentication

@objc protocol HGBioMetricsDelegate

{

func biometricsPassed(viewController:HGBioMetrics);

func biometricsFailed(viewController:HGBioMetrics);

func biometricsCancelled(viewController:HGBioMetrics);

}

@objcMembers // All class members will be exposed to Objective-C

class HGBioMetrics: UIViewController,UserNamePasswordDisplayLogic {

@IBOutlet var successLabel: UILabel!

@IBOutlet var biometricIdImage: UIImageView!

var delegate: HGBioMetricsDelegate?

let myContext = LAContext()

override func viewDidLoad() {

super.viewDidLoad()

if HGBioMetrics.biometricType() == .touch {

self.biometricIdImage.image = UIImage.init(named: "touchId")

}

else if HGBioMetrics.biometricType() == .face {

self.biometricIdImage.image = UIImage.init(named: "faceId")

}

else {

self.biometricIdImage.isHidden = true

}

self.setup()

self.perform(#selector(HGBioMetrics.launchBiometricsAuthentication), with: nil, afterDelay: 2.0)

// 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.

}

private func setup()

{

biometricIdImage.clipsToBounds = false

biometricIdImage.layer.shadowColor = UIColor.white.cgColor

biometricIdImage.layer.shadowOpacity = 1

biometricIdImage.layer.shadowOffset = CGSize.zero

biometricIdImage.layer.shadowRadius = 10

}

@IBAction func goBack(sender:UIButton) {

self.myContext.invalidate()

self.delegate?.biometricsCancelled(viewController: self)

}

func launchBiometricsAuthentication() {

biometricIdImage.isHidden = true;

let myLocalizedReasonString = "Biometric authntication required to proceed. Kindly put your thumb on \"Home Button\" or Sign-In"

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.successLabel.text = "User authenticated successfully"

//self.login(email: "onboard14@gmail.com", password: "India1947")

self.delegate?.biometricsPassed(viewController: self)

} else {

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

self.successLabel.text = evaluateError?.localizedDescription

// Call your sign-in module here

}

self.biometricIdImage.isHidden = false;

}

}

} else {

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

successLabel.text = "Could not evaluate policy."

self.delegate?.biometricsFailed(viewController: self)

self.biometricIdImage.isHidden = false;

}

} else {

// Fallback on earlier versions

self.delegate?.biometricsFailed(viewController: self)

successLabel.text = "Ooops!!.. This feature is not supported."

self.biometricIdImage.isHidden = false;

}

}

@IBAction func touchIdAction(_ sender: UIButton) {

self.launchBiometricsAuthentication()

}

}

extension HGBioMetrics {

static func biometricType() -> BiometricType {

let authContext = LAContext()

if #available(iOS 11, *) {

let _ = authContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)

switch(authContext.biometryType) {

case .none:

return .none

case .touchID:

return .touch

case .faceID:

return .face

}

} else {

return authContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) ? .touch : .none

}

}

enum BiometricType {

case none

case touch

case face

}

}


 

In Above code start scanning the Thumb/Face after 2 seconds. If you want to scan on view load the remove the perform selector.


8 views0 comments

Recent Posts

See All

Comments


Call

Mob: 9972946755

 

Contact:

akarotech@gmail.com

Bengaluru, Karnataka

India

 

  • facebook-square
  • twitter-bird2-square

Follow Us

 

© 2019 by Akaro Technologies (Quality Values Our Work)

bottom of page