Integration of Google Sign in with Swift

Posted By : Satish Phogat | 27-Jun-2017

In this blog , I am going to emplain the process of Google Sign - In using Swift 3

The easiest way of installation is using pod file .Open terminal and create pod file for particular project .Write 

   pod 'Google/SignIn'

Enter this line in pod file and exicute pod install command in terminal.

Get config. file by entering project name and bundle identifier and download GoogleService-info.plist file .Drag this downloaded file to project . It will create bridging header file .

Enter below line to bridging header file.

#import<Google/SignIn.h>

Go to project => info => Url Types and add two types and enter REVERSED_CLIENT_ID and BUNDLE_ID from downloaded info.plist.

Now add GIDSignInDelegate delegate to appdelegate class 

Enter below code to didFinishLaunchingWithOptin method in app delegate

var configureError: NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error : \(configureError)")
        
        GIDSignIn.sharedInstance().delegate = self
Enter below code to your appdelegate class 
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }
Add below three function to app delegate class
func signIn(signIn: GIDSignIn!, didSignInForUser googleUser: GIDGoogleUser!,
                withError error: NSError!)   {}

func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
                withError error: NSError!) {}

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {} 
Add GIDSignInUiDelegate delegate to view controller class and add below line to view did load function
 GIDSignIn.sharedInstance().uiDelegate = self

Due to this delegate view for google sign in will be visible.

We will have to add these three function in our view controller 

public func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!){
        self.present(viewController, animated: true, completion: nil)

    }
    
    public func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!){
    }
    
    public func sign(inWillDispatch signIn: GIDSignIn!, error: Error!){
           }

This is whole about Google Sign In.

Thanks.

About Author

Author Image
Satish Phogat

Satish Phogat is IOS developer in Oodles Technologies. He is a good learner and Team Player.

Request for Proposal

Name is required

Comment is required

Sending message..