Google Sign in SDK integration for ios apps


Google plus login lets users login into your iOS app with their existing Google account and get their profile information like name, email and other details. By integrating google plus login in your apps, you can get all the user details in one shot.The major advantage of integrating G+ login is, you can drive more users to your app by providing quicker & easiest way of signup process.

So let’s start by doing required setup.

Step 1: Set up your CocoaPods dependencies

Google Sign-In uses CocoaPods to install and manage dependencies. Open a terminal window and navigate to the location of the Xcode project for your application. If you have not already created a Podfile for your application, create one now:

  1. Open your terminal and type CD “—project path—”
  2. Type “pod init” , now Podfile will be created in your project, and open that pod file by using command “open podfile
  3. Now in the podfile, type command like   pod ‘GoogleSignIn’  ” to install all requirement frameworks like “Google signIn”, “GoogleToolboxForMac”, “GTMOAuth2”, “GTMSessionFetcher”…etc

podInstallation

 

Now in Terminal,  while installing all above frameworks it will be looks like below screenshot.

podInstallation_2

 

This creates an .xcworkspace file for your application. Use this file for all future development on your application.

Step 2: Get Google Sign-in Client ID:

Google sign in Client ID is an ID that allows us to make calls to Google API within our iOS based application.

To get ClientID follow below steps:

  1. Sign into Google Developers Console with the help of your Google Account.
  2. 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.
  3. 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.
  4. Whenever you click on “Enable button”, it will sanction Google Sign in for your app.
  5. Whenever you enable Google Sign-in for your app, click -> Continue button that will generate configuration files for you.
  6. 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.

Now open downloaded plist .and it will be like the below screenshot.plist

Step 3: Add a URL scheme to your project:

Google Sign-in requires a custom URL Scheme to be added to your project. To add the custom scheme:

  1. Open your project configuration: double-click the project name in the left tree view. Select your app from the TARGETS section, then select the Info tab, and expand the URL Types section.
  2. Click the + button, and add your reversed client ID as a URL scheme.
    The reversed client ID is your client ID with the order of the dot-delimited fields reversed. For example you can check in above screenshot.
  3. When completed, your config should look something similar to the following (but with your application-specific values):

URLSchemes

4. Now that you’ve downloaded the project dependencies and configured your Xcode project, you can add Google Sign-In to your iOS app.

Step 4: Enable SignIn:

(A) To enable sign in, you must configure the GIDSignIn shared instance. You can do this in many places in your app. Often the easiest place to configure this instance is in your app delegate’s application:didFinishLaunchingWithOptions: method.

  1. In your app delegate’s .h file, declare that this class implements the GIDSignInDelegate protocol.

gidProtocol

(B) In your app delegate’s application:didFinishLaunchingWithOptions: method, configure the GIDSignIn shared instance and set the sign-in delegate.

didFinishLaunching

(C) Implement the application:openURL:options: method of your app delegate. The method should call the handleURL method of the GIDSignIn instance, which will properly handle the URL that your application receives at the end of the authentication process.

applicationOpenURL

(D) For your app to run on iOS 8 and older, also implement the deprecated application:openURL:sourceApplication:annotation: method.

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation 
{
  return [[GIDSignIn sharedInstance] handleURL:url
                             sourceApplication:sourceApplication
                                    annotation:annotation];
}

(E) In the app delegate, implement the GIDSignInDelegate protocol to handle the sign-in process by defining the following methods which are showing in below screenshot.

AppDelegate

Step 5: Add the sign-in button

Next, you will add the Google Sign-In button so that the user can initiate the sign-in process. Make the following changes to the view controller that manages your app’s sign-in screen:

(A) In the App Delegate.h file, add the Google Sign-In interface and declare that this class implements the GIDSignInUIDelegate protocol.

In ViewController.h file copy below this.

viewController

(B) In the view controller., override the viewDidLoad method to set the UI delegate of the GIDSignIn object, and (optionally) to sign in silently when possible.

viewController(m)

(C) In these examples, the view controller is a subclass of UIViewController. If in your project, the class that implements GIDSignInUIDelegate is not a subclass of UIViewController, implement the signInWillDispatch:error:, signIn:presentViewController:, and signIn:dismissViewController: methods of the GIDSignInUIDelegate protocol. For example you can add below code .

viewController(m)_1

(D) Add a GIDSignInButton to your storyboard, XIB file, or instantiate it programmatically. To add the button to your storyboard or XIB file, add a View and set its custom class to GIDSignInButton.

Storyboard

(E) If you want to customize the button, do the following:

    • In your view controller’s .h file, declare the sign-in button as a property.
      @property(weak, nonatomic) IBOutlet GIDSignInButton *signInButton;
    • Connect the button to the signInButton property you just declared.
    • Customize the button by setting the properties of the GIDSignInButton object.

Step 6: Sign out the user

You can use the SignOut method of the GIDSignIn object to sign out your user on the current device, for example:

In ViewController.h, copy below code:

- (IBAction)didTapSignOut:(id)sender 
{
  [[GIDSignIn sharedInstance] signOut];
}

Step 7: Retrieving user information

Once the user has authenticated and authorized access to the scopes you request, you can access user information through the GIDGoogleUser object.

Copy Below Code in ViewController.m class :

viewController(m)_2

Now Run your application and Click on Login button on simulator, it will be redirected to the gmail sign in page, there you have to enter email id and password, and these credentials will be secured and this screen something looks like below screenshots.

googleSignIn

After submission of your credentials click on “NEXT” button and now the new page will be opened with a title of accounts.google.com and this page will asks you like “are you want to access your google account?” And stating with your email id. Now below in this page you can see two buttons as CANCEL and ALLOW.

Now Click on ALLOW, and again you will be redirected to another screen where you will find all the details of your google account such as name email id, phone number , date of birth..etc.

googleSignIn_2

You can get your client ID, UserId, Full Name, Email ID..etc in your console.. the same I am showing in simulator for your reference.

google_SignIn_3