Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Integrate Google Login in iOS App Using Swift?

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 765
    Comment on it

    Login with google is very common functionality in iOS applications. We can Implement google login in swift 3.0 either by using pods or google SDK.

    We need to install the following pod-

    pod 'Google/SignIn'

    Then register your app on the google developer account. After registration, you need to follow some steps as provided in the registration process. We need to add the GoogleService.plist file in the project and make some change in the URL schemes of the project.

    After that, we need to make changes in the AppDelegate file of your project as following -

    
    import Google
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            GIDSignIn.sharedInstance().clientID = "YOUR_CLIENT_ID"
            var configureError: NSError?
            GGLContext.sharedInstance().configureWithError(&configureError)
            assert(configureError == nil, "Error configuring Google services: \(configureError)")
    
            return true
        }
    

     

    Now add the following function in AppDelegate.swift file -

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        return GIDSignIn.sharedInstance() .handle(url,sourceApplication: options [UIApplicationOpenURLOptionKey .sourceApplication] as ? String, annotation: options [UIApplicationOpenURLOptionKey.annotation])
                                                     
    }
    

     

    Now Create the Class which will manage the Google Sign In Process and write the following code in the class -

    import UIKit
    import GoogleSignIn
    
    class GoogleManager: NSObject, GIDSignInDelegate {
        
        var signInBlock:CompletionBlock?
        
        func googleSignIn(completion: @escaping CompletionBlock)  {
            self.signInBlock = completion
            GIDSignIn.sharedInstance().delegate = self
            GIDSignIn.sharedInstance().signOut()
            GIDSignIn.sharedInstance().signIn()
        }
        
        //Google SignIn Delegates
        func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
            if error == nil {
                let userId = user.userID                  // For client-side use only!
                let idToken = user.authentication.idToken // Safe to send to the server
                let fullName = user.profile.name
                //let givenName = user.profile.givenName
                //let familyName = user.profile.familyName
                let email = user.profile.email
                let googleDetails = ["googleUserID":userId, "token":idToken, "name":fullName, "emailAddress":email]
                if signInBlock != nil {
                    self.signInBlock!(true, googleDetails as AnyObject?, nil)
                }
                
            } else { 
                print("\(error.localizedDescription)")
                if signInBlock != nil {
                    signInBlock!(false, nil, error)
                }
            }
        }
        
        func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
            if signInBlock != nil {
                signInBlock!(false, nil, error)
            }
        }
        
    }
    

     

    Here We use the completion block as following -

    typealias CompletionBlock = (Bool , AnyObject? , Error?) -> Void

     

    Now Call the uiDelegate from your view controller where we actually want to implement login functionality.

    GIDSignIn.sharedInstance().uiDelegate = self

    You can get the data in your viewcontroller by using SignIn block which we used in GoogleMnager Class.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: