Thursday, 3 October 2013

Google+ iOS SDK 1.4.0 with native sharing

I'm really happy to see the release today of version 1.4.0 of the iOS Google+ SDK. It's worth a look, in no small part because the team have fixed one of the longest standing requests for the SDK: the ability to share to Google+ without the user leaving the app. In previous versions of the SDK, the only sharing option was via the mobile browser, so the user would be switched out of then back into the app they were using. This release puts the share box fully inside the app for signed in users, and has also given it a bunch of new special powers for better sharing. All of the documentation on the Google+ iOS developer site has been revised and improved, but I wanted to highlight my favourite parts of the release here as well.

Native Sharebox

The new share box is a smoother way for users to share from within your application to Google+. The existing share box that calls out to the browser and back is still available for anonymous users, but if you're implementing Google+ Sign-In, go native share box all the way! Both implement the GPPShareBuilder interface, so your code (generally) doesn't have to care.

The change is really extremely easy - drop in the new SDK which you can download LINK from the Google+ developers site, and replace your current call to the shareDialog function with nativeShareDialog.

There are some new required packages to include in your linker settings as well. The complete required set is:

  • AssetsLibrary.framework
  • Foundation.framework
  • CoreLocation.framework
  • CoreMotion.framework
  • CoreGraphics.framework
  • CoreText.framework
  • MediaPlayer.framework
  • SystemConfiguration.framework
  • UIKit.framework

You'll of course also need to include the Google+ SDK files themselves!

  • GoogleOpenSource.framework (or individual open source files)
  • GooglePlus.framework
  • GooglePlus.bundle


Here's what it look in xcode.

Make sure you include the bundle as well! Else it might fail to find the font and give you an error like: "*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Google Plus fallback bundle could not be found. Please include it in the project.'". You can add it in build phases "copy bundle resources" in Xcode:

Upload images and videos

The new share box includes the ability to directly attach videos and images into posts, so they are displayed as if they had been shared directly onto Google+. The video call takes a local URL, and the image a UIImage. Note that both of these are only available with the native share box, so you will have to cast your GPPShareBuilder to a GPPNativeShareBuilder to call them.

For example, to attach an image to a post:

New Error Callback

While the old finishedWithShare handler is still available, there's a new callback available in GPPShareDelegate that gives more details on errors that happen. Rather than passing in a BOOL, it follows the common model of passing an NSError that is set to nil on success. The new, replacement method is finishedSharingWithError:error. This allows you to detect the share succeeding, failing, or being cancelled:

It's also worth noting that there are some conditions under which the native sharebox wont open, for example if you try to attach both a URL and an image. You should check the return from the open call. If it's No, the sharebox is probably misconfigured, and an error will be logged to the console.

Note that along with this, you also have an ability to close any open share dialogs - this can be helpful if an important event happens in your app, and you need to bring the user right back. As an example, the below code just arbitrarily closes the share box after 5 seconds, which is a feature you might want to implement if you particularly dislike your users.

Prefill Recipients

The native share box also adds the ability to prefil recipients, so you can target posts to specific people. This is generally for no more than about 10 recipients, and you target them with an array of IDs, such as could be retrieved from the retrieve people call.

ID token

One of the other complaints developers have had is that its hard to authenticate an iOS client to a server with the current SDK. With this release, full ID token support is included which allows authenticating a backend, without giving it access to call Google APIs. Tim Bray's excellent blog post from the Android blog shows how to verify back ends for Android clients - now with this release you can share that approach between Android, the web, and iOS.

It's actually rather straightforward to get the token once the user has signed-in.

https://gist.github.com/ianbarber/6813428

The token itself looks like this. It has three dot-separated (I've tried to highlight the dots a bit!) base64 encoded sections which contain a header, a JSON blob, and a signature that allows you to cryptographically verify it was created by Google.

eyJhbGciOiJSUzI1NiIsImtpZCI6ImRjZjY2NGI3YjkyOWRmOWU3NDFmNWZhNGNjNzQyYTg3MTRhZWFiNDcifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwic3ViIjoiMTEzMzQwMDkwOTExOTU0NzQxNjk2IiwiYXVkIjoiMzY2NjY3MTkxNzMwLWo0ZnU4cG52YzJqMXR0a3JsMWs3anU1bjVwaW0zdmV0LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXpwIjoiMzY2NjY3MTkxNzMwLWo0ZnU4cG52YzJqMXR0a3JsMWs3anU1bjVwaW0zdmV0LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXRfaGFzaCI6IlZpcWdrOTlhT21DUW5xREh6WFo2MVEiLCJpYXQiOjEzODA3OTg3MTEsImV4cCI6MTM4MDgwMjYxMX0.MMkyQ-O5F8HlLW2bRwpf4xMWR67slIm5Cu8PAdoNGPbcGZ562sJ03wq4foy5d2zSW6BGZWWkPOwNvFq-NF93fMQeR2QBISF2l_BZ0Laxiufn8vXaoqWwRdD8XEfayF0m-Wx5JlkWgkmFPbYfriaAU7vfKGltHy7Cg42srDgTbEM

Decoding the body lets us see the application that was signed in to and the user ID. If we had requested the users's email address, that would be in here as well. This allows you to authenticate the user on your application servers without having to make any other API calls, which is pretty cool!

{
 "issuer": "accounts.google.com",
 "issued_to": "366667191730-j4fu8pnvc2j1ttkrl1k7ju5n5pim3vet.apps.googleusercontent.com",
 "audience": "366667191730-j4fu8pnvc2j1ttkrl1k7ju5n5pim3vet.apps.googleusercontent.com",
 "user_id": "113340090911954741696",
 "expires_in": 3510,
 "issued_at": 1380798711
}

Some Other Helpful Things

The UIActivityView is a really nice way to integrate sharing, and you'll find in the sample app a helpful example custom UIActivity and share icon you can use in your own applications - it's ShareActivity in the GooglePlusSample, and ShareSheetMask in the resources folder.

There have also recently been several improvements to the Google+ iOS app (and the mobile browser sign-in interface) which may benefit your users. For example, you may notice that when signing in you now have the option to choose an account or register a new one, to make life easier for users with multiple accounts. If you want to offer a switch in your own app, the easier way is just to call [[GPPSignIn sharedInstance] signOut] to clear the local state, then [[GPPSignIn sharedInstance] authenticate] to sign in again.

Special thanks from me to +Banjo, +musiXmatch and +allthecooks for trying out the SDK and sharing useful feedback pre-release: go check out their apps (all of which will be featuring the new sharebox soon!)