Replace REDIRECT_URI_SCHEME with your CLIENT_ID using reverse domain notation. For example: com.googleusercontent.apps.1234-abcd.
Handle redirection after user signs in to obtain oAuth tokens (access, ID and refresh) by calling getTokenResponse function on GoogleSignIn.Controller.
For iOS app, you can do it by implementing this function in your UIApplicationDelegate:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
controller.getTokenResponse(using: url) { result in
if case let .success(response) = result {
print("ACCESS TOKEN: \(response.accessToken)")
}
}
return true
}
If you are using UIWindowSceneDelegate in your app, implement this function instead:
func scene(_ scene: UIScene, openURLContexts contexts: Set<UIOpenURLContext>) {
guard let redirectUrl = contexts.first?.url else { return }
controller.getTokenResponse(using: redirectUrl) { result in
if case let .success(response) = result {
print("ACCESS TOKEN: \(response.accessToken)")
}
}
}
GoogleSignIn-Swift
Minimalistic Google Sign In oAuth 2.0 client written in Swift
Install
GoogleSignIn-Swift is compatible with Swift Package Manager. You can add it as a dependency to your Xcode project by following official documentation.
Use
You will need to configure Google oAuth 2.0 client
In your project create
GoogleSignIn.ControllerinstanceYou can obtain
CLIENT_IDandCLIENT_SECRETfrom Google API Console.REDIRECT_URIis yourCLIENT_IDwith reverse domain notation and://suffix. For example:1234-abcd.apps.googleusercontent.comcom.googleusercontent.apps.1234-abcdcom.googleusercontent.apps.1234-abcd://Implement presenting sign in page to the user, by opening
signInPageURLprovided by the controller.It can vary depending on the type of the app you are working on. The simpliest way to do it in iOS app is by using
UIApplication.open(_ url:):Configure your application, so it can handle redirect after sign in.
For iOS app, you can do it by adding or modifying
CFBundleURLTypesinInfo.plist:Replace
REDIRECT_URI_SCHEMEwith yourCLIENT_IDusing reverse domain notation. For example:com.googleusercontent.apps.1234-abcd.Handle redirection after user signs in to obtain oAuth tokens (access, ID and refresh) by calling
getTokenResponsefunction onGoogleSignIn.Controller.For iOS app, you can do it by implementing this function in your
UIApplicationDelegate:If you are using
UIWindowSceneDelegatein your app, implement this function instead:Do whatever you want with obtained access token.
License
Copyright © 2019 Dariusz Rybicki Darrarski
License: GNU GPLv3