GoogleSignInSwift
GoogleSignInSwift
uses OAuth 2.0 to to obtain a users Google authentication credentials and/or their profile information. GoogleSignInSwift
is written 100% in Swift. You can find more information about Google OAuth 2.0 sign in protocol here
Installation
GoogleSignInSwift
is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'GoogleSignInSwift'
Usage
Setup
Provide GoogleSignIn
with your apps Google API Client ID and any Google API scope
GoogleSignIn.shared.clientId = "<Google API Client ID>"
GoogleSignIn.shared.addScope("<Google API Scope>")
Sign in
GoogleSignIn.shared.delegate = self
GoogleSignIn.shared.presentingWindow = view.window
GoogleSignIn.shared.signIn()
listen for completion by conforming to GoogleSignInDelegate
func googleSignIn(didSignIn auth: GoogleSignIn.Auth?, user: GoogleSignIn.User?, error: Error?) {
if let error = error {
print("Error signing in: \(error)")
return
}
// Route user to app
}
Use Access Token
Although you can access the token via GoogleSignIn.shared.auth?.accessToken
it is recommended you use GoogleSignIn.shared.refreshingAccessToken
. If the token is expired, this method will refresh and return a new valid token.
GoogleSignIn.shared.refreshingAccessToken { token, _ in
guard token != nil else {
return
}
// Use token
}
Sign out
GoogleSignIn.shared.signOut()
Fetch profile
GoogleSignIn.shared.getProfile { user, error in
//
}
note: this is done for you once by default when user logs in.
Get user email with sign in
To get a users email, make sure that you set this before sign in.
GoogleSignIn.shared.email = true
You can also disable profile
(default true
)
Auth status
GoogleSignIn.shared.isSignedIn
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Author
License
GoogleSignInSwift is available under the MIT license. See the LICENSE file for more info.