Swiftpack.co -  seanmcneil/BiometricAuthentication as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
seanmcneil/BiometricAuthentication
Lightweight wrapper around Biometric Authentication for easy integration with SwiftUI
.package(url: "https://github.com/seanmcneil/BiometricAuthentication.git", from: "1.0.1")

BiometricAuthentication

This package is a light wrapper around the Apple framework for local authentication via device biometrics that easily supports integration with SwiftUI based apps.

While supporting common usage, this package also includes the ability to mock the authentication tools, making it easy to test this functionality in your apps.

Requirements

  • iOS 13
  • macOS 10.15
  • Xcode 12

Usage

Initialize

First, initialize an instance of BiometricAuthentication with the String values you wish to use for the cancel and reason prompts that may be presented during your authentication flow. This object should be an instance property, otherwise it could go out of scope if used in a function.

let auth = BiometricAuthentication(cancelTitle: "Cancel Title", 
                                   reason: "Reason Title")

Observing Status

You can observe a user's current status through the authenticationAction property of BiometricAuthentication.

The following shows all potential values that authenticationAction can have:

switch action {
case .authenticated:
    // Handle authentication

case .notAuthenticated:
    // Handle sign out

case .error(let error):
    switch error {
    case .authenticationFailed:
        // User was denied access
        
    case .unsupportedHardware:
        // The device does not support biometrics
    }
}

Login

To perform a login operation:

auth.login()

Upon a successful login, the authenticationAction value will be authenticated.

Authentication Errors

If a user fails to properly authenticate, you will receive a BiometricError, published through the authenticationAction property.

These are:

public enum BiometricError: Swift.Error {
    case unsupportedHardware
    case authenticationFailed
}
  • unsupportedHardware will be encountered when the user's device does not support biometrics
  • authenticationFailed will be encountered when the user failed to authenticate via biometrics

Logout

To perform a logout operation:

auth.logout()

Upon a successful logout, the authenticationAction value will be notAuthenticated.

However, if the user is already notAuthenticated, then no change will occur.

Enable Face ID with your app

For Face ID to work with your app, you must also place an entry for NSFaceIDUsageDescription in your app's info.plist file, otherwise it will not work.

<key>NSFaceIDUsageDescription</key>
<string>Use Face ID instead of a password to access your account.</string>

Testing

If you wish to run tests against the package, you can utilize the ContextProtocol to mock the LAContext class. Examples of this are included with the package's tests.

Currently, the package is achieving a test coverage of 99%.

Author

Sean McNeil

License

BiometricAuthentication is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API