by Onur Tuna

How to set up Google Sign In SDK with Swift for iOS

This post is a clearer explanation of the implementation presented in the Google Developer tutorial. The Google tutorial recommends you to use pod, however, I don’t like to use pod — I want more freedom. So, this tutorial installs and sets the SDK manually.

The Google tutorial wrote their sample project in Objective-C. At the end of this post, you can find a sample project written in Swift.

Let’s start

Now we’re going to install the latest SDK from the Google Developers page. Here in this tutorial, the SDK version is 4.0.1. You can use any version but I recommend using the latest one.

When you download the SDK you’re going to see the following files and folders:

  • CHANGELOG.md
  • GoogleAppUtilities.framework
  • GoogleSignIn.bundle
  • GoogleSignIn.framework
  • GoogleSignInDependencies.framework
  • GoogleSymbolUtilities.framework
  • README.md
  • Sample: This is a sample project written in Objective-C.

Now create a project using Xcode. We’re going to link all necessary frameworks to it. Put the frameworks located in the SDK folder anywhere you want. I prefer collecting all the libraries in a folder called Library under my main Projects folder.

Open your project and go to Build Settings. Enter the path where your frameworks are located.

Next copy the GoogleSignIn.bundle file by drag-dropping into the project. You have to drag and drop the frameworks too but do not copy them.

We need two more frameworks: Safari Services and System Configuration. Apple provides them. You can link them in the Build Phases > Link Binary with Libraries.

The last thing you should do in this part is to add a linker flag. Add the flag below in Build Settings > Other Linker Flags:

$(OTHER_LDFLAGS) -ObjC

Configuration File

It’s time for getting a configuration file for your project. You should start an app in the Google Developer page. However, you’ll not copy the configuration file. Instead, keep it anywhere — some information in it might be needed later.

Enter the page to create a project.

Choose an app name and give your bundle identifier which you can find under General in Xcode. In the next page, you’ll enable sign in for your app by clicking the Enable Sign In button. After all that, download the GoogleService-Info.plist file. Keep it anywhere you want.

Go back to your Xcode project. Find your reversed client id in the plist file you have just downloaded. Paste it to the Info > URL Types.

Add Google sign in to app

Google Sign In SDK is an Objective-C library so you need a bridging header in order to bind it to your Swift project. You can create a bridging header manually. However, you can also leave it Xcode do this automatically.

Create a new .m file with a dummy name. It will ask you to create a bridging header — say yes. Remove the .m file, you don’t need it. Import the Google Sign In in your bridging header.

#import “GoogleSignIn/GoogleSignIn.h”

Now, go to you app delegate file named AppDelegate.swift. Your app delegate should look like something like this.

It seems like a lot of code. However most of it is written by default when you create a new project.

Let me explain the changes. Your class — AppDelegate — now implements GIDSignInDelegate protocol. In order to conform the delegate we have implemented some methods: application:openURL:options: and signIn:signIn:didSignInForUser:withError:. Also we have configured GIDSignIn object in the application:didFinishLaunchingWithOptions: method. The rest is not important.

One significant issue is that you should paste your client id in the application:didFinishLaunchingWithOptions: method. You can find your client id in the plist file we downloaded.

Sign in button

We can add a button and watch our app working. Go to your ViewController.swift. The final code should look something like below:

Only one line of code has been added. However, pay attention that our class implements the GIDSignInUIDelegate protocol. We need a button to make the user click. Go to your Story board and put a view on it. Drag and drop a UIView. Set GIDSignInButton as the base class and you’re done.

Now run the app and login. You’re finished with the basics. You can use Google Login in your apps from now on. In case you have any problems do not hesitate to contact me.

Example Codes

onurtuna/Google-Signin-Example
Google-Signin-Example - Google Sign in example using Swift 3github.com