Authentication and Authorization in Swift (iOS/macOS): Overview of Tools and Libraries
In modern iOS/macOS development, security and access control are critically important aspects. Swift provides both built-in frameworks for local authentication and integration with cloud services. In this article, we will cover the main tools and libraries for implementing login, registration, and user permission verification.
1. LocalAuthentication — Face ID / Touch ID / Apple Watch
The LocalAuthentication framework allows you to authenticate a user via biometrics or device passcode. It works on iOS and macOS.
Main Features:
- Checking biometric availability (Face ID, Touch ID)
- Authentication with context (LAContext)
- Using the device passcode as a fallback option
Code Example:
import LocalAuthentication
func authenticateUser(completion: @escaping (Bool, Error?) -> Void) { let context = LAContext() var error: NSError?
// Check if biometrics are available guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else { completion(false, error) return }
// Start authentication context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Confirm your identity to access data") { success, authError in DispatchQueue.main.async { completion(success, authError) } }}2. Firebase Authentication — Cloud Authentication
Firebase Auth is a powerful library that supports login via email/password, Google, Apple, GitHub, and other providers. It is ideal for server-based applications.
Installation via Swift Package Manager:
// In Package.swift add:// .package(url: "https://github.com/firebase/firebase-ios-sdk.git", from: "10.0.0")// target: "YourAppTarget", dependencies: ["FirebaseAuth"]Registering a New User:
import FirebaseAuth
func signUp(email: String, password: String) { Auth.auth().createUser(withEmail: email, password: password) { authResult, error in if let error = error { print("Registration error: \\(error.localizedDescription)") return } print("User registered: \\(authResult?.user.uid ?? "")") }}Signing In an Existing User:
func signIn(email: String, password: String) { Auth.auth().signIn(withEmail: email, password: password) { authResult, error in if let error = error { print("Login error: \\(error.localizedDescription)") return } print("Login successful: \\(authResult?.user.email ?? "")") }}3. Keychain Services — Secure Token Storage
To store JWT tokens, passwords, or keys, use Keychain. This is a native iOS/macOS mechanism.
Example of Saving and Reading:
import Security
func saveToKeychain(key: String, data: Data) -> Bool { let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrAccount as String: key, kSecValueData as String: data ] SecItemDelete(query as CFDictionary) let status = SecItemAdd(query as CFDictionary, nil) return status == errSecSuccess}
func readFromKeychain(key: String) -> Data? { let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrAccount as String: key, kSecReturnData as String: true, kSecMatchLimit as String: kSecMatchLimitOne ] var result: AnyObject? let status = SecItemCopyMatching(query as CFDictionary, &