How to Login with Google Plus in Swift

Posted By : Manu Gupta | 30-Jun-2015

In this blog I will tell you how to login with Google Plus in Swift which is very important nowadays for quick signup as users don't have time to fill the forms.

 

Google Plus is providing is SDK which is written in Obj-C and we will implement this in our swift project using the Bridging Header file. Below will be the step by step guide to implement the login.

  • Download the GOOGLE+ SDK and plaese check the requirements mentioned on the link.
  • Go to this link Create a new project and enter the name of your project.
  • Now after that on the left side menu Go to APIs & auth -> APIs -> Enabled APIs and then make sure that Google+ API status is set to ON.
  • After enabling the APIs go to consent screenn and enter the email and product name of your app
  • Now go to credentials tab under "APIs & auth" menu
  • Click on create new client ID and after that select installed application , iOS , enter the bundle ID of your app and click on create.

After this you should be able to see your client id and client secret,

 

  • Now go to your Xcode project manually drag the "GooglePlus.Framework" and "GoogleOpenSource.Framework" to your project.
  • Now Create the Bridging-Header.h file and import the following files into that header
                   #import 
                   #import 
                 
  • Now import the following files into your view controller
                        import AddressBook
                        import MediaPlayer
                        import AssetsLibrary
                        import CoreLocation
                        import CoreMotion
                    
  • Go to your info.plist file and do the following shown in the image
  • Execute this code on the button click
                
                        var googleSignIn: GPPSignIn!
                        googleSignIn = GPPSignIn.sharedInstance();
                        googleSignIn.shouldFetchGooglePlusUser = true;
                        googleSignIn.clientID = "enter your client id";
                        googleSignIn.shouldFetchGoogleUserEmail = true;
                        googleSignIn.shouldFetchGoogleUserID = true;
                        googleSignIn.scopes = [kGTLAuthScopePlusLogin];
                        googleSignIn.delegate = self;
                        googleSignIn.authenticate();
                        
                
  • Extend the "FBSDKLoginButtonDelegate"
                    class ViewController: UIViewController,GPPSignInDelegate {
                
  • Finally, add the delegate methods of GPPSignInDelegate
                    func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) {
                        if auth.accessToken != nil {
                        println(auth.accessToken)
                    }
                }
                
  • And to Logout from google+ simply call this method on button click
                    googleSignIn.signOut()
                

 

 

About Author

Author Image
Manu Gupta

Manu is a Native iOS (Swift) and Titanium lead developer . Manu has good experience working with Swift and Alloy framework as well. He loves watching movies and travelling.

Request for Proposal

Name is required

Comment is required

Sending message..