The TouchID authentication mechanism implemented in Swift.

Overview

iOS-TouchID-Swift

The TouchID authentication mechanism implemented in Swift

About

In iOS 8, Apple provides a new framework named LocalAuthentication which lets us use the TouchID biometric authentication mechanism to our applications. TouchID was first introduced in iOS 7 as an alternative way to unlock iPhone 5s devices and make purchases in the App Store. Here I have created a "plug and play" class written in Swift, which you can add to your projects and integrate the TouchID authentication to your applications without performing any custom implementation at all, even without you need to know the TouchID details at all.

Normally, you don't have to do any modification to the TouchID class, unless you really need to. Read next to see instructions about its usage, and how easy it is to integrate it to your projects.

How To Use

Add the class file to the project

The first step is to add the TouchID.swift file to your project. This file imports the LocalAuthentication framework, which the compiler should link automatically. However, if you encounter any problems, then go and add manually the LocalAuthentication.framework to your project.

Adopt the protocol

In the view controller that you are about to integrate the TouchID authentication, adopt the TouchIDDelegate protocol as shown next:

class ViewController: UIViewController, TouchIDDelegate { ... }

Initialize, setup and present

In the viewDidLoad method (or anywhere else that best suits you) initialize a TouchID object, set it up and make the proper call to present the TouchID dialog:


// Initialize a TouchID object.
let touchIDAuth : TouchID = TouchID()

// Set the reason for which you ask authentication and that will appear to the TouchID dialog.
touchIDAuth.touchIDReasonString = "To access the app."

// Set your view controller class as the delegate of the TouchID class.
touchIDAuth.delegate = self

// Present the TouchID dialog.
touchIDAuth.touchIDAuthentication()

Handling authentication results

There are two delegate methods you should necessarily implement. In case of successful authentication, the touchIDAuthenticationWasSuccessful delegate method is called, and in here you should add any code needed to proceed to the app and display its contents.

In case of unsuccessful authentication, then the touchIDAuthenticationFailed delegate method is called. Its parameter, an enum value, describes the reason for which the authentication failed. Use a switch or an if statement to go through all possible cases and take the proper actions for each one of them. Pay special attention to the case where the user fallbacks to a custom authentication method. For all the possible fail/error reasons, either take a look at the TouchIDErrorCode enum in the TouchID.swift file, or look up the LAError class in the official documentation.

Remarks

Take a quick look at the sample project for a reference on how to use it. It won't take you more than two minutes, and it's a necessary step so as you see everything that I previously described in action.

Remember that you can't test the TouchID authentication in Simulator, you need a real device.

You might also like...
CryptoSwift is a growing collection of standard and secure cryptographic algorithms implemented in Swift
CryptoSwift is a growing collection of standard and secure cryptographic algorithms implemented in Swift

CryptoSwift Crypto related functions and helpers for Swift implemented in Swift. (#PureSwift) Note: The master branch follows the latest currently rel

RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

BlueRSA Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux

RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

BlueRSA Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux

Swift-problem-solving - Swift 알고리즘 맛보기 😋

swift-problem-solving Swift 로 알고리즘 익히기 😆 Programmers 난이도 풀이 문제 바로가기 Lv.2 오픈채팅방 링크 Lv.3 다단계 칫솔 판매 링크 Lv.3 합승 택시 요금 링크 Leetcode 난이도 풀이 문제 바로가기 Medium 1

Swift-cuckoo-collections - Cross-platform Swift dictionaries & sets that use a cuckoo hashing algorithm

CuckooCollections A Swift package for open-addressed sets and dictionaries that

Helps you define secure storages for your properties using Swift property wrappers.

🔐 Secure Property Storage Helps you define secure storages for your properties using Swift property wrappers. 🌟 Features All keys are hashed using S

Swift cross-platform crypto library using CommonCrypto/libcrypto

BlueCryptor Swift cross-platform crypto library derived from IDZSwiftCommonCrypto. IMPORTANT NOTE: This release is NOT entirely source code compatible

A wrapper for Apple's Common Crypto library written in Swift.

IDZSwiftCommonCrypto A Swift wrapper for Apple's CommonCrypto library. IDZSwiftCommonCrypto works with both CocoaPods and Cathage. For more details on

A framework for the JOSE standards JWS, JWE, and JWK written in Swift.

JOSESwift is a modular and extensible framework for the JOSE standards JWS, JWE, and JWK written in Swift. 💡 Please note that this implementation of

Owner
Gabriel Theodoropoulos
Gabriel Theodoropoulos
An iOS passcode lock with TouchID authentication written in Swift.

PasscodeLock A Swift implementation of passcode lock for iOS with TouchID authentication. Originally created by @yankodimitrov, hope you're doing well

Serge Velikan 203 Dec 6, 2022
Framework for biometric authentication (via TouchID) in your application

Features Requirements Communication Installation Usage Intro Biometric authentication availability Feature enabled/disabled for biometric authenticati

Igor Vasilenko 29 Sep 16, 2022
Use Apple FaceID or TouchID authentication in your app using BiometricAuthentication.

BiometricAuthentication Use Apple FaceID or TouchID authentication in your app using BiometricAuthentication. It's very simple and easy to use that ha

Rushi Sangani 804 Dec 30, 2022
TouchID used easy on one line in your ViewController.

TouchIDExtension TouchID used easy on one line in your ViewController. ##Installation At this moment, You can install only a way, manually. For instal

Joan Molinas 65 Feb 26, 2020
TouchEncryptedJson - Simple project that accepts an input and encrypts it with the TouchID on a Mac

TouchEncryptedJson Simple project that accepts an input and encrypts it with the

Charles Edge 2 Aug 29, 2022
Very simple swift wrapper for Biometric Authentication Services (Touch ID) on iOS.

SimpleTouch Very simple swift wrapper for Biometric Authentication Services (Touch ID) on iOS. Sample Project There is a SimpleTouchDemo target define

Simple Machines 117 Nov 15, 2022
LocalAuth - Another Fusion library to implement the local authentication using Biometry

FusionLocalAuth Another Fusion library to implement the local authentication usi

Vedant Jha 0 Jan 13, 2022
The minimalistic, secure and open-source two-factor authentication app.

Einmal /ˈainmaːl/ German: once The minimalistic, secure and open-source two-factor authentication app. Features ♻️ Cross-platform — available on Andro

Incipher 75 Aug 21, 2022
Two-Factor Authentication Client for iOS

Authenticator Two-Factor Authentication Client for iOS. Authenticator is a simple, free, and open source two-factor authentication app. It helps keep

Matt Rubin 770 Dec 30, 2022
An easy-to-use, open-source two-factor authentication app designed specifically for iOS.

Tofu An easy-to-use, open-source two-factor authentication app designed specifically for iOS. Tofu generates one-time passwords to help you protect yo

Calle Luks 380 Jan 8, 2023