Due to the emergence of M-commerce mania, most of the operations are carried out through mobile applications. Mobiles are the medium that let user communicate with the world regardless of time and place. Nowadays, applications are leveraging users to chat, send images, video and voice recordings, Emails and many other kinds of stuff. So today, Indian App Developers will focus on how to integrate Google Sign-in SDK in an iOS application using Swift to make access easier.
Google Sign-in SDK impulsively handles out the sign-in process. Whenever you tap on the button it will ask the user to enter the signing credential for his/her Google account. Initially, you won't be able to see the sign in button in UI storyboard but, it will appear during run-time.
The main objective of this post is to explain how Google sign in option can be implemented in your iOS application.
Implementing Google Sign-in SDK
Step 1: Introduction
Google SignIn SDK allows:
- Users to log in via Google account.
- Get user’s profile information details.
Here, I will explain how user can login to app through Google account but for that you need to have your account first.
To sign in to Google sign in you need to have clientID.
Step 2: Get Google Sign-in Client ID
Google sign in Client IDis an ID that allows us to make calls to Google API within our iOSbased application.
To get ClientID follow below steps:
- Sign into Google Developers Console with the help of your Google Account.
- If you have not created any app then create it. If created, then choose your app and add it to bundle identifier. Now choose your country and then click to continue Button to proceed ahead.
- Select the Google service that you want to make use of. Now, Select Google Sign-in & click -> ENABLE GOOGLE SIGN-IN to enable Google sign in service.
- Whenever you click on “Enable button”, it will sanction Google Sign in for your app.
- Whenever you enable Google Sign-in for your app, click -> Continue button that will generate configuration files for you.
- Now, you will be navigated to download the configuration file. Click -> Download GoogleService-info.plist button to download the configuration file. This file will be used for later purposes.
Step 3: Project Configuration
- Create a newer Xcode Project.
- Add GoogleService-Info.plist to the source of Xcode project.
- A new window will appear that helps you to choose the options to add files. Check “Copy items if needed” checkbox & click ->Finish button.
- Setup URL Types : Used to handle callbacks from Google.
3.1 Here Are the Steps
- Find GoogleService-Info.plist
- Copy REVERSED_CLIENT_ID
- Just select your project from Navigator
- Select your app from “Project and Target List”
- Go -> Info and Expand -> URL Types
- Click -> '+' button to add URL Type
- Now Paste REVERSED_CLIENT_ID to URL Schemes
- Click '+' button to add another URL Type.
- Now paste your project “ Bundle Identifier”.
3.2 Add Other Frameworks
Google sign in also requires another framework such as:
To add up this framework in your project select project from Project Navigator and click -> Build Phases. Expand the Link Binary With Libraries and click -> '+' button. Now you can choose and add needed frameworks.
3.2 Add Bridging Header
Use: Bridging Header helps us to make us of Objective-C code in our swift application.
Below is the code to bridge header file :
Step 4: Sign in Using Google
1. Open AppDelegate.swift and use below lines of codes in the method named application(_didFinishLaunchingWithOptions)
Here I would be making use of my API Key to integrate SDK in iOS app. You need to implement your key in the code.
2. Now Add your AppDelegate.swift class in the subclass of
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate
3. Now open AppDelegate.swift and add below lines in the method named application(_openURL:,_options:)
This method is used to handle and maintain the URL that you receive at the end of the application process.
GIDSignInDelegate() method inside AppDelegate.swift
This method is invoked when a user successfully signs into his account. You can retrieve user’s basic information through this method.
5. Add “Google Sign-in Button” to perform a sign-in process.
a. Make your ViewController in subclass of
class ViewController: UIViewController, GIDSignInUIDelegate
b).Add below code to
viewDidLoad() method of your ViewController.
GIDSignIn.sharedInstance().uiDelegate = self
c. Now Add UIView to the storyboard & Select the view and add GIDSignInButton as a custom class.
d. Now declare an IBOutlet in your ViweController
@IBOutlet weak var signInButton: GIDSignInButton!
e. Now connect IBOutlet with the UIView created in the storyboard
As per now, you are unable to look anything in the
UIView in storyboard though you have added
GIDSignInButton as the custom class. But once you run the app “Google Sign-in button” will be visible in the view during run time. Google Sign-in SDK handles and manages the sign-in process. Whenever you tap on the button it will ask for credentials of Google Account.
Step 5: Sign Out From Google Account
Below method needs to be called out to signout from Google Account:
Hope that you find the blog posted by me helpful when you begin your work with Google SignIn SDK in iOS. Let me know in comments if you have any concerns regarding Google Sign in iOS.