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
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.
- 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).
- Now from the ios others, select an Empty File.
- Now hit next and name your file.
- 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).
- 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.
- 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.
- After selecting the app and providing the Bundle ID click on continue.
- Then Enable Google SignIn.
- Now you will get the configuration file.
- 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.
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.
- 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.
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.
- 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 🙂
Leave a Reply