RubyMotion & Google iOS SDK

23 September
  • technical, ruby, rubymotion, ios, google

G+ logo I needed a way to login with Google+ in the RubyMotion iOS app I’m building. After having used the Facebook iOS SDK, I expected it to be relatively painless.

It turns out there is very little up to date information about connecting the Google iOS SDK to a RubyMotion app. I looked through lots of examples without much success.

I ended up having to look through the Google iOS SDK documentation which, although very good, is naturally targeted at Objective-C development.

Add the pod

First thing is to add the ‘googleplus-ios-sdk’ pod to your Rakefile. This seems obvious but I tried a bunch of other things before this.

app.pods do
  pod 'googleplus-ios-sdk'
end

After a rake pod:install, this gives you access to the GPPSignIn class which contains a shared singleton instance. That shared instance keeps track of what’s going on.

Popping up the login screen

gplus = GPPSignIn.sharedInstance
gplus.clientID = "0000000000.apps.googleusercontent.com"
gplus.shouldFetchGooglePlusUser = true
gplus.scopes = [ 'profile', 'email' ]
gplus.delegate = self
gplus.authenticate # this will pop the google login screen

This is configuring the interaction with Google. You’ll need to set up a google Client for iOS applications. I won’t go through this as this is described in the Getting starting documentation.

The scopes define the data that you are wanting to get access to. When the user comes back to the app, the finishedWithAuth method in whatever you set as the delegate will end up being called. Before we get there, our app needs to handle being opened from a URL.

Setting up URL scheme

Before implementing anything on return, we need to tell our app what URL schemes to respond to. The value needs to match the Bundle ID value you put into your Client ID for iOS application. In your app this needs to be set in your Rakefile like this.

app.info_plist['CFBundleURLTypes'] = [{ 
  'CFBundleURLName' => 'com.companyname.appname',
  'CFBundleURLSchemes' => ['com.companyname.appname'] 
}]

Coming back to the app after login

To login, the user is sent away from the app to login (they’re sent to a browser or the google plus app, I believe). This means that we have to handle the user coming back into the app. This is done through a URL scheme. We need to do this in the app_delegate.rb like this.

def application(application, openURL: url, 
      sourceApplication: sourceApplication, 
      annotation: annotation)
  GPPURLHandler.handleURL(url, 
        sourceApplication: sourceApplication, 
        annotation: annotation)
end

As you can see, we use the GPPURLHandler to handle the values. This will then callback to the finishedWithAuth in the initiating controller.

def finishedWithAuth(auth, error:error)
  if error
    App.alert("Failed to login using Google+")
  else
    # Maybe do something with the auth.accessToken
  end
end

Done

So you can see, it’s not too hard once you know how!

I think I’ll leave it there for now. I might add a follow up post about how to retrieve data using the access token.