OAuth Client Library for Swift (iOS/macOS)
OAuth (Open Authorization) is an open authorization protocol that allows applications to obtain limited access to user resources without transmitting their login and password. In the Apple ecosystem (iOS, macOS, watchOS, tvOS), implementing OAuth 2.0 is the standard for integrating with services such as Google, Facebook, GitHub, Twitter, and many others. For Swift developers, there are several proven libraries that simplify the authentication and token management process.
In this article, we will look at one of the most popular and flexible libraries — OAuthSwift, and also discuss an alternative — AppAuth-iOS (recommended by Google). You will learn how to connect the library, perform authorization, obtain an access token, refresh token, and store them securely.
Key Features of OAuthSwift
- Support for OAuth 1.0 and OAuth 2.0
- Built-in WebView support (SFSafariViewController, WKWebView)
- Automatic retrieval and renewal of access tokens
- Support for various grants (Authorization Code, Implicit, Client Credentials, Resource Owner Password)
- Easy integration with Alamofire, URLSession, and other networking frameworks
- Asynchronous callbacks (completion handlers) and Combine support (via extensions)
- Secure token storage via Keychain
Installing OAuthSwift
The library is installed via Swift Package Manager (SPM) or CocoaPods. Example for SPM:
// In Xcode: File → Add Packages...// URL: https://github.com/OAuthSwift/OAuthSwift.git// Version: 2.2.0 and aboveOr via Package.swift:
dependencies: [ .package(url: "https://github.com/OAuthSwift/OAuthSwift.git", from: "2.2.0")]Integration Example with OAuth 2.0 (Google)
Let's consider a typical scenario — authorization via Google to obtain an access token. First, you need to register your application in the Google Cloud Console and obtain a client_id and client_secret.
Step 1. Configure URL scheme
In the Info.plist file, add a scheme for the callback:
<key>CFBundleURLTypes</key><array> <dict> <key>CFBundleURLSchemes</key> <array> <string>com.googleusercontent.apps.YOUR_CLIENT_ID</string> </array> </dict></array>Step 2. Authorization code
import OAuthSwift
let oauthswift = OAuth2Swift( consumerKey: "YOUR_CLIENT_ID", consumerSecret: "YOUR_CLIENT_SECRET", authorizeUrl: "https://accounts.google.com/o/oauth2/auth", accessTokenUrl: "https://oauth2.googleapis.com/token", responseType: "code")
// Save a reference to the controller for presenting the WebViewlet handler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)oauthswift.authorizeURLHandler = handler
let state = generateState(withLength: 20)oauthswift.authorize( withCallbackURL: URL(string: "com.googleusercontent.apps.YOUR_CLIENT_ID:/oauth2callback")!, scope: "https://www.googleapis.com/auth/userinfo.email", state: state, parameters: [:]) { result in switch result { case .success(let (credential, response, parameters)): print("Access Token: \\(credential.oauthToken)") print("Refresh Token: \\(credential.oauthRefreshToken)") // Save tokens to Keychain KeychainWrapper.standard.set(credential.oauthToken, forKey: "access_token") KeychainWrapper.standard.set(credential.oauthRefreshToken, forKey: "refresh_token") case .failure(let error): print("Authorization error: \\(error.localizedDescription)") Also in library
Task Queues for Rust: Overview of Popular Libraries and Tools
Working with Excel in TypeScript: Libraries, Examples, and Tools
Elasticsearch Client for Java: Complete Guide to Connection and Work
Task Queues in Node.js: Bull, Bee-Queue and Kue