Google+ in Swift

Fernando Olivares
Swift Programming
Published in
3 min readOct 15, 2014

I’m working on this top-secret app and one of the requirements is that we include G+ integration using Swift. I haven’t found any good tutorials or explanations about making it work, so I thought I’d share what I’ve found so far. You can find the completed project, here.

Do Step 1: Creating the Google Developers Console Project, then Step 2.1 and then come back here.

2.2 — Drag the three files (GoogleOpenSource.framework, GooglePlus.bundle and GooglePlus.framework) to your project.

In this case, I did copy the files to my project and I added them to my Supporting Files folder. For now, these frameworks are unable to be reached by Swift but we’ll be working on that in the next few steps.

2.3 — Make sure the frameworks are added in your project’s Linked Framework and Libraries.

Select your project and go into the General tab. Scroll to the bottom and just make sure that both of Google’s frameworks are there. Probably paranoia, but I just wanted to make sure.

2.4 — Add the URL type

In the Info tab, go to the bottom and add a new URL Type. This should be your bundle identifier.

2.5 — Create a new header file and name it Bridging-Header (New File > Header File > Bridging-Header.h), then import both of Google’s frameworks.

This header file is the key to unlock the Objective-C classes. This serves as a way for Swift to understand what Objective-C classes it needs to import.

2.6 — This Bridging-Header.h file needs to be referenced in your Build Settings. Make sure you point to the correct path, otherwise you’re going to crash. The path is usually projectName/Bridging-Header.h

Once we have the Bridging-Header in our project, we need to let the project know that this is the place where Swift has to look for our Obj-C classes.

2.7 — Add the “-ObjC” flag to Other Linker Flags. If you have any problems with this step, make sure to double check it. For some reason, when I added it, it added two separate entries.

The “-ObjC” flag makes it so that categories inside the Google Frameworks are also imported.

2.9 — Your view controller in charge of log in, should implement GPPSignInDelegate and its methods.

As you know, implementing a protocol in Swift is as easy as adding it after the parent class. The protocol methods are just direct ports from the ones declared in the GPPSignInDelegate protocol. I’ve re-written them all as optionals because I’m not certain if they are required. The compiler does require them, so I’d just leave it like that unless it crashes.

2.10 — Import the following libraries: AddressBook, MediaPlayer, AssetsLibrary, CoreLocation and CoreMotion

These libraries are required by the framework to work. I suspect you will probably need to import them whenever you work with any of the G+ classes.

2.11 — Declare a GPPSignIn variable and set it up in your viewDidLoad method.

This variable will be in charge of the actual signing in. You need to pass it the clientID that you got in Step 1: Creating the Google Developers Console Project.

2.12 — Finally, in your app delegate, implement the openURL method.

This is required so that the app can handle log in if you’re coming from Safari or the G+ app.

2.13 — After all of this, your app will compile and run but you will hit an error screen if you try to authenticate.

What you need to do is go back to your Developer Console and go to APIs & Auth > Consent Screen and fill that out. Specifically, the product name.

Select an email address and write down your product name, then scroll down and save.

That’s it!

You’ll get a callback to one of your methods in ViewController when you either accept or decline. From this point forward, you would remove the last line in your viewDidLoad method and implement the sign in steps in the Google+ Sign-in for iOS page if you wanted the G+ sign in button.

Good luck!

--

--

Fernando Olivares
Swift Programming

I do iOS (remotely) and I also say things. Tap me @olivaresf.