Simplified iOS

  • Home
  • About
  • Contact
  • Advertise
  • Write for Us

Google Sign In Integration in iOS using Swift Example

June 2, 2017 by Belal Khan Leave a Comment

We always create a user registration in our application. But often people hates filling forms. And here comes Social Signin that makes the life easier. So it is a good idea to put login with some social network in your application. I guess almost everyone uses Google, so integrating google login in your application is an important thing. That is why I am posting this Google Sign In Integration in iOS Example. So lets see how we can integrate Google Sign In in our iOS App using Swift.

Contents

  • 1 Google Sign In Integration in iOS Video
  • 2 Creating a new Xcode Project
  • 3 Adding CocoaPods dependencies
  • 4 Getting a Configuration File
  • 5 Adding a URL Scheme
  • 6 Adding Google Sign In
    • 6.1 Share this:
    • 6.2 Related

Google Sign In Integration in iOS Video

  • You can go through this video as well. Here you will see the steps in details about Google Sign In Integration in iOS.

Creating a new Xcode Project

  • First create a new Xcode Project.

google sign in integration project

  • Now we will add CocoaPods dependencies for GoogleSignin.

Adding CocoaPods dependencies

  • Right click on the project and select new file. (As shown in the image below).

Google Sign In Integration in iOS

  • Now from the ios others, select an Empty File.

Google Sign In Integration

  • Now hit next and name your file.

podfile

  • Make sure you have given the name as Podfile.
  • Now write the following in the file. GoogleSigninExample is the project name make sure it matches with your project name.
target "GoogleSigninExample" do

    pod 'Google/SignIn'

end
  • Now open terminal and go to your project directory and run the following command.
pod install
  • It will install the dependencies.
  • Now close the Xcode and in terminal type open YourProjectName.xcworkspace. (See the image below).

install pods

  • Now we need to get a configuration file from google.

Getting a Configuration File

  • Go to this link, and click on GET A CONFIGURATION FILE button.

get configuration file

  • Now it will take you to Google Developers. Here you need to Select an Existing Google App or you can also create a new Google App. You need to provide the Bundle Identifier of your application here as well. Just see the below image for help.

choose app

  • After selecting the app and providing the Bundle ID click on continue.
  • Then Enable Google SignIn.

enable google signin

  • Now you will get the configuration file.

google service

  • You will get a file named GoogleService-Info.plist.
  • Now you need to add this file into your project. See the image to know where it should be added.

googleservice file

Adding a URL Scheme

  • Now we need to add a URL scheme to our project. For this first select GoogleService-Info.plist file and copy REVERSED_CLIENT_ID as shown in the image.

url scheme

  • Now click on your project and select Info and inside URL Types you need to add two things. First one is what you just copied and the other one is the Bundle Identifier. You can see the below image for reference.

url types

Adding Google Sign In

  • Now we will add a Google Sign In Button to our ViewController. For this come inside the ViewController.swift file and write the following code.
//
//  ViewController.swift
//  GoogleSigninExample
//
//  Created by Belal Khan on 02/06/17.
//  Copyright © 2017 Belal Khan. All rights reserved.
//

import UIKit
import Google
import GoogleSignIn

class ViewController: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {

    //label to display name of logged in user
    @IBOutlet weak var labelUserEmail: UILabel!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //error object
        var error : NSError?
        
        //setting the error
        GGLContext.sharedInstance().configureWithError(&error)
        
        //if any error stop execution and print error
        if error != nil{
            print(error ?? "google error")
            return
        }
        
        
        //adding the delegates
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().delegate = self
        
        //getting the signin button and adding it to view
        let googleSignInButton = GIDSignInButton()
        googleSignInButton.center = view.center
        view.addSubview(googleSignInButton)
        
    }
    
    //when the signin complets
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        
        //if any error stop and print the error
        if error != nil{
            print(error ?? "google error")
            return
        }
        
        //if success display the email on label 
        labelUserEmail.text = user.profile.email
    }


}

  • Now come inside AppDelegate.swift and add the following function.
//
//  AppDelegate.swift
//  GoogleSigninExample
//
//  Created by Belal Khan on 02/06/17.
//  Copyright © 2017 Belal Khan. All rights reserved.
//

import UIKit
import Google
import GoogleSignIn

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    //this function is added only
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url as URL!,
                                                 sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }
  • Now test the application in the Simulator.

Google Sign In Integration in iOS using swift

  • Bingo! it is working absolutely fine. If you are still having confusions you can get my source code from the below link.

[sociallocker] Google Sign In Integration in iOS Source Code [/sociallocker]

Also Read: Xcode Login Screen Example using Swift 3, PHP and MySQL

So thats all for this Google Sign In Integration in iOS tutorial friends. Hope you found it helpful. For queries lets have a discussion in the comment sections. And don’t forget to SHARE the post. Thank You 🙂

Share this:

  • Tweet
  • Share on Tumblr
  • WhatsApp

Related

Filed Under: iOS Development Tutorial Tagged With: Google Sign In Integration in iOS, google signin swift

About Belal Khan

I am Belal Khan, I am currently pursuing my MCA. In this blog I write tutorials and articles related to coding, app development, iphone etc.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *




About Me

belal khan simplified ios

Hello I am Belal Khan, founder and owner of Simplified iOS. I am currently pursuing MCA from St. Xavier's College, Ranchi. Apart from my academic I am a blogger, I run various websites and majority of them are about coding and development.

Connect with Me

Follow Simplified iOS

Simplified iOS

Popular Posts

  • Swift SQLite Tutorial for Beginners – Using… (99,467)
  • Swift PHP MySQL Tutorial – Connecting iOS App… (98,354)
  • UIWebView Example to Load URL in iOS using Swift in Xcode (80,016)
  • Download Full High Sierra Installer to Create Bootable USB (70,346)
  • Xcode Login Screen Example using Swift 3, PHP and MySQL (67,312)
  • How to Format USB on Mac? Formatting External Hard… (61,392)
  • Swift JSON Tutorial – Fetching and Parsing… (59,414)
  • Firebase Realtime Database Tutorial for Swift using Xcode (53,721)
  • iOS Registration Form Example using PHP and MySQL (48,989)
  • Xcode Text Field Tutorial for iOS Application using Swift (41,228)




About

Simplified iOS is a blog where you can find latest tutorials related to coding and app development for iphone and MAC OS. Here you can get Simplified iPhone, iPad and Mac OS development tutorial to know the things about Apple Development from basics to advanced level.

Quick Links

  • Advertise
  • Contact
  • Disclaimer
  • Privacy Policy
  • Write for Us

Copyright © 2017 · Simplified iOS· All rights Reserved. And Our Sitemap.All Logos & Trademark Belongs To Their Respective Owners·