Generic selectors
Exact matches only
Search in title
Search in content

Example for Apple’s Touch ID Fingerprint API for Swift and iOS

09/04/2014 / 0 Comments

Swift is the new programming language which was published by Apple in XCode 6. It can be used for Cocoa and Cocoa Touch, on iOS as well as on OS X.
Swift code can be applied side-by-side with Objective-C. It is designed to assist developers in writing secure and reliable codes. This includes mechanisms which provoke that all variables are initialized before the first access, that domains are checked for overflows as well as automatic storage management. Furthermore, Swift is optimized for runtime performance and is closely connected with Xcode and Xcode Playgrounds. Thus, all developer tools and debuggers can be provided. For more information about Swift please visit Apple´s developer sites.

The Touch ID Fingerprint API for Swift will be available under iOS 8. It enables user authentication by the use of the fingerprint sensor. This enables the realization of logins, the encryption of user data and storage in the keychain.

We recently integrated the new Touch ID fingerprint verification as biometric login procedure into our SecSign ID app.  SecSign ID offers users a secure login procedure for websites and programs and eliminates the need for passwords. The principle on which the SecSign ID is based is a physical two-factor authentication (2FA).

Requirements for the use of Swift and the Touch ID Fingerprint API for Swift:

  • XCode 6
  • iPhone 5s with iOS 8


The iOS Touch ID authentication takes place in three steps:

  • The check whether the Touch ID Fingerprint API is available
  • The validation of the fingerprint
  • The validation of the fingerprint or the device´s PIN


Swift sample code how to use Touch ID:

@import LocalAuthentication;
import LocalAuthentication
// Get the local authentication context:
var context = LAContext()
var error : NSError?
// Test if TouchID fingerprint authentication is available on the device and a fingerprint has been enrolled.
if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error:&error) {
	// evaluate
	var reason = "Authenticate for server login"
	context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: {
		(success: Bool, authenticationError: NSError?) -> Void in
		// check whether evaluation of fingerprint was successful
		if success {
			// fingerprint validation was successful
			println("Fingerprint validated.")
		} else {
			// fingerprint validation failed
			// get the reason for validation failure
			var failureReason = "unable to authenticate user"
			switch error!.code {
				case LAError.AuthenticationFailed.toRaw():
					failureReason = "authentication failed"
				case LAError.UserCancel.toRaw():
					failureReason = "user canceled authentication"
				case LAError.SystemCancel.toRaw():
					failureReason = "system canceled authentication"
				case LAError.PasscodeNotSet.toRaw():
				 	failureReason = "passcode not set"
				case LAError.UserFallback.toRaw():
					failureReason = "user chose password"
					failureReason = "unable to authenticate user"
			println("Fingerprint validation failed: \(failureReason).");
} else {
	//get more information
	var reason = "Local Authentication not available"
	switch error!.code {
		case LAError.TouchIDNotAvailable.toRaw():
			error = "Touch ID not available on device"
		case LAError.TouchIDNotEnrolled.toRaw():
			reason = "Touch ID is not enrolled yet"
		case LAError.PasscodeNotSet.toRaw():
			error = "Passcode not set"
		default: error = "Authentication not available"
	println("Error: Touch ID fingerprint authentication is not available: \(reason)");

Further information

You can find a detailed description in the blog „How to Use Apple‘s Touch ID Fingerprint API in Your Mobile App“. You will also find there a description of the integration of the Touch ID Fingerprint API and the keychain under iOS

And we would love to hear your feedback. To see the code in action, please watch our YouTube demo video showcasing the world’s first Apple Touch ID web login. And you can visit our website to learn more about our current SecSign ID solution and download the current version of our app in the iTunes App Store.

SecSign 2FA