This is a drop-in class to handle AppAuth with Google Services (iOS & macOS).
Just add this dependency to your Podfile:
pod GAppAuth
The transitive dependency to GTMAppAuth is added automatically.
Add GTMAppAuth
dependency to your Podfile (Cocoapods) or copy the files manually to your project directory. Add GAppAuth.swift
to your project and set-up you project as follows to use AppAuth with Google Services.
- Setup your project (APIs & Services -> Credentials -> Create Credentials -> OAuth Client ID -> iOS) at https://console.developers.google.com to retrieve ClientID and iOS scheme URL.
- Enable Google APIs as desired.
- Add ClientId and RedirectUri to your Info.plist:
<key>GAppAuth</key>
<dict>
<key>RedirectUri</key>
<string>com.googleusercontent.apps.YOUR-CLIENT-ID:/oauthredirect</string>
<key>ClientId</key>
<string>YOUR-CLIENT-ID.apps.googleusercontent.com</string>
</dict>
- Add custom URL-Scheme to your project:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.YOUR-CLIENT</string>
</array>
</dict>
</array>
- Setup your project (APIs & Services -> Credentials -> Create Credentials -> OAuth Client ID -> Other) at https://console.developers.google.com to retrieve ClientID and ClientSecret.
- Enable Google APIs as desired.
- Add ClientId, ClientSecret RedirectUri to your Info.plist:
<key>GAppAuth</key>
<dict>
<key>RedirectUri</key>
<string>com.googleusercontent.apps.YOUR-CLIENT-ID:/oauthredirect</string>
<key>ClientId</key>
<string>YOUR-CLIENT-ID.apps.googleusercontent.com</string>
<key>ClientSecret</key>
<string>YOUR-SECRET</string>
</dict>
- Add custom URL-Scheme to your project:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.YOUR-CLIENT</string>
</array>
</dict>
</array>
Note: Make sure Sandboxing is turned off or properly configured, otherwise it's not possible to open the Browser window.
- In order to authorize for any Google Service, you'd need to append the respective scope to the authorization request via:
GAppAuth.shared.appendAuthorizationRealm
(i.e. kGTLRAuthScopeDrive for Google Drive access). - From any
UIViewController
orNSViewController
start the authorization workflow by callingGAppAuth.shared.authorize
. - You might want to retrieve any existing authorization upon start of the app which can be done via
GAppAuth.shared.retrieveExistingAuthorizationState
. - There are two closures you can monitor in order to be notified about any changes
stateChangeCallback
or errorserrorCallback
.
Note: In case of a revoked access by the user, both callbacks will be called.
Feel free to create issues or open up a PR.