Introduction
Welcome to the documentation for the GoogleSignIn framework. This page provides detailed information on how to integrate Google Sign-In functionality into your iOS app using the GoogleSignIn SDK.
Prerequisites
Before you begin integrating Google Sign-In, make sure you have completed the following prerequisites:
- An iOS app project created in Xcode
- A valid Google API Console project with the necessary configuration
- A GoogleService-Info.plist file added to your project
- Cocoapods installed on your local machine
Installation
To install the GoogleSignIn framework using Cocoapods, follow these steps:
- Open your terminal and navigate to your project’s directory.
- Run the command
pod init
to create a podfile for your project. - Open the podfile using a text editor and add the following line:
pod 'GoogleSignIn'
- Save the podfile and run the command
pod install
in the terminal. - Wait for Cocoapods to install the GoogleSignIn framework.
- Once installation is complete, open your Xcode workspace.
Configuration
Before you can start using Google Sign-In, you need to configure your app in the Google API Console and add the necessary configuration files. Follow these steps:
- Go to the Google API Console and create a new project if you haven’t already.
- Select your project from the dropdown menu in the top bar.
- Navigate to the “Credentials” page in the sidebar.
- Click on the “Create credentials” button and select “OAuth client ID” from the dropdown.
- Configure the consent screen by providing a product name and other optional details. Save your changes.
- Select “iOS” as the application type for your OAuth client ID.
- Enter your bundle identifier in the “Bundle ID” field.
- Copy the generated “Client ID” value, as you will need it later in your code.
- Click the “Create” button to generate your credentials.
- Download the GoogleService-Info.plist file by clicking the “Download GoogleService-Info.plist” button.
- Add the GoogleService-Info.plist file to your Xcode project by dragging it into the root folder of your project in Xcode.
Usage
To use the GoogleSignIn framework in your app, follow these steps:
- Import the GoogleSignIn framework at the top of your Swift file:
import GoogleSignIn
- In your AppDelegate.swift file, add the following methods to handle the GoogleSignIn callback URLs:
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url)
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return GIDSignIn.sharedInstance().handle(url)
}
- In your UIViewController, create an instance of GIDSignIn, usually in your viewDidLoad method:
let signIn = GIDSignIn.sharedInstance()
- Implement the GIDSignInDelegate and add the necessary delegate methods to handle the sign-in process:
class YourViewController: UIViewController, GIDSignInDelegate {
// Add necessary delegate methods such as didSignInFor and didDisconnectWith in this class
}
- Set the delegate of your GIDSignIn instance:
signIn.delegate = self
- Call the sign-in method whenever you want to initiate the Google Sign-In process:
signIn.signIn()
Conclusion
Congratulations! You have successfully integrated Google Sign-In into your iOS app using the GoogleSignIn framework. You can now allow users to sign in with their Google accounts and access their associated information.