OAuth client for Swift (iOS/macOS): OAuthSwift library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

OAuth client for Swift (iOS/macOS): OAuthSwift library



OAuthSwift is a powerful and flexible library for implementing the OAuth 1.0 and OAuth 2.0 protocol in Swift applications for iOS, macOS, watchOS, and tvOS. It provides developers with a simple and intuitive interface for integrating third-party services (Google, GitHub, Facebook, Twitter, and others) without the need to manually handle complex HTTP requests, redirects, and token exchange. The library is entirely written in Swift, supports modern language versions (Swift 5+), and is actively maintained by the community.



The main advantage of OAuthSwift is minimizing boilerplate code. The developer only needs to specify the client ID, client secret, authorization and redirect URLs, and the library itself manages the process of obtaining an access token. OAuthSwift supports all common OAuth 2.0 grants: Authorization Code, Implicit, Client Credentials, and Resource Owner Password Credentials. For OAuth 1.0, full support for HMAC-SHA1, RSA-SHA1, and Plaintext signature methods is implemented.



Installation and Setup

The library is distributed via Swift Package Manager (SPM), CocoaPods, and Carthage. The recommended method is SPM:

// In Xcode: File → Swift Packages → Add Package Dependency// URL: https://github.com/OAuthSwift/OAuthSwift.git// Version: 2.2.0 and above

Import into code:

import OAuthSwift


Basic OAuth 2.0 Implementation (using Google as an example)

Let's look at a complete example of authorization via Google using the Authorization Code Flow. First, you need to create a project in the Google Cloud Console, obtain a client ID, and configure a redirect URI (e.g., com.example.app:/oauth-callback).



class GoogleAuthManager {    private var oauthSwift: OAuth2Swift?

func startGoogleAuth() { 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" )

// Configure request parameters let state = generateState(withLength: 20) let scope = "https://www.googleapis.com/auth/userinfo.profile"

// Start authorization oauthSwift?.authorize( withCallbackURL: URL(string: "com.example.app:/oauth-callback")!, scope: scope, state: state, parameters: [:], headers: nil ) { result in switch result { case .success(let (credential, response, parameters)): print("Access Token: \\(credential.oauthToken)") print("Refresh Token: \\(credential.oauthRefreshToken)") self.fetchUserInfo(credential: credential) case .failure(let error): print("Authorization error: \\(error.localizedDescription)") } } }

private func fetchUserInfo(credential: OAuthSwiftCredential) { let url = "https://www.googleapis.com/oauth2/v2/userinfo" oauthSwift?.client.get(url, parameters: [:]) { result in switch result { case .success(let response): if let json = try? JSONSerialization.jsonObject(with: response.data) as? [String: Any] { print("Email: \\(json["email"] ?? "")") print("Name: \\(json["name"] ?? "")") } case .failure(let error): print("Error fetching data: \\(error)") } } }}


Handling URL Scheme (iOS)

For the callback URL to work correctly, you need to configure AppDelegate or SceneDelegate:

// AppDelegate.swiftfunc applic                    

Recommendations