Authorization and Authentication in Swift: Overview of Tools and Libraries
In iOS and macOS development, user data security is priority number one. Authentication (identity verification) and authorization (granting access rights) are basic components of any modern application. In this article, we will review key tools, libraries, and Swift frameworks for implementing login via Apple ID, third-party services (Google, Facebook), as well as local token storage.
The article is aimed at developers familiar with the basics of Swift and Xcode. All code examples are tested on Swift 5.9+ and are compatible with iOS 17+/macOS 14+.
1. AuthenticationServices: Apple Sign In
Native authorization via Apple ID is a mandatory App Store requirement for applications using third-party providers (Google, Facebook). The AuthenticationServices framework provides a simple API for implementing Sign in with Apple.
import AuthenticationServicesimport SwiftUI
struct SignInWithAppleView: View { var body: some View { SignInWithAppleButton( onRequest: { request in request.requestedScopes = [.fullName, .email] }, onCompletion: { result in switch result { case .success(let authResults): // Get user token guard let credential = authResults.credential as? ASAuthorizationAppleIDCredential, let tokenData = credential.identityToken, let token = String(data: tokenData, encoding: .utf8) else { return } print("Apple token: \\(token)") case .failure(let error): print("Error: \\(error.localizedDescription)") } } ) .signInWithAppleButtonStyle(.black) .frame(width: 280, height: 44) }}Important: Apple returns fullName and email only on the first login. For subsequent authorizations, use userID from credential.user.
2. Firebase Authentication: Multi-Provider Authentication
Firebase Auth is a cross-platform solution for authorization via email, Google, Facebook, GitHub, and other providers. It includes ready-made UI components and integration with Firestore.
Installation via Swift Package Manager:
// Package.swift or via Xcode: File > Add Packages// URL: https://github.com/firebase/firebase-ios-sdk.git// Select FirebaseAuth, FirebaseFirestoreExample of email/password login:
import FirebaseAuth
func signInWithEmail(email: String, password: String) { Auth.auth().signIn(withEmail: email, password: password) { authResult, error in if let error = error { print("Auth error: \\(error.localizedDescription)") return } guard let user = authResult?.user else { return } print("User signed in: \\(user.uid)") }}
func createUser(email: String, password: String) { Auth.auth().createUser(withEmail: email, password: password) { authResult, error in // Handle registration }}For Google Sign-In, the GoogleSignIn SDK is additionally used. Firebase automatically manages sessions and token refresh.
3. Keychain Services: Secure Token Storage
Storing JWT tokens or refresh tokens in UserDefaults is a serious security mistake. Use Keychain (encrypted storage on iOS/macOS).
Wrapper for Keychain:
import Security
class KeychainManager { static func save(key: String, data: Data) { let query: [String: Any] = [ kSecClass as String: kSecClassGener Also in library
Kotlin Application Monitoring: Libraries and Tools for Performance Tracking
Bouncy Castle for Java: Complete Guide with Examples
Working with Excel in Swift: A Complete Guide to Libraries and Tools for iOS/macOS