Notes: Sharing to Google+ on IOS

It has been a long time since I touched any IOS code, so I was quite looking forward to a chance to do some when the IOS team within Google+ released the SDK into preview at Google I/O last month. Luckily, the documentation on the SDK page on is excellent, and covers the basics of the setup, but in part because I've forgotten almost everything about XCode and IOS, I thought it best to just write down what I did and what I encountered, primarily as an Aide-mémoire for the future. Anyone building a new app should follow the documentation - but perhaps some developers will hit an issue I did and get something out of this post!

First things first, I had to set up a new OAuth2 client id at the API developer console with permissions for the Google+ API (I also added the Google+ History API, though this app does not use it so far). Next I created an OAuth2 client id for an installed application. It's important to make sure that the bundle ID specified here matches the one in Xcode, or the OAuth process wont work properly!

In Xcode (for reference, this was version 4.2), I created a new project, and following the docs added the Security and SystemConfiguration frameworks, and added the ObjC and -all_load flag to my linker settings. This is done by going to the target for you project, selecting Build, changing the display option from Basic to All and looking under the Linking panel.

Trying to drag up specifics of actually writing an Objective-C was quite a challenge, but I did manage to include the GooglePlusShare.h file in my main controller .h file, define an property for the GooglePlusShare object itself, and instantiate it it in the viewDidLoad function, using the clientid from the oauth2 console. The client ID I stored in the AppDelegate, wrapping a static NSString with a class function, as the sample app supplied with the SDK does. That can be retrieved in the controller by adding the app delegate header, and calling the clientId function. 

And in the controller, this can be retrieved like this:

To get the basic project building, I had to create a new group and add in the Google+ lib files (libraries and headers) from the lib directory, and the contents of the OpenSource directory in the SDK, which I split into the GTL and GTM folders mostly for aesthetics:

However, I had a number of errors of the sort "GTLBatchResult.m:31:26:{31:68-31:79}: error: ARC forbids explicit message send of 'autorelease' [4]", which the FAQ suggests can be avoided with adding the -fno-objc-arc  flag to the Compile Sources under Build Phases. The entry field is found by doubleclicking on the file - or selecting a bunch and hitting enter. 

That got the basic app compiling, but to actually do something requires a few more steps. Firstly I added a sharing function to the controller that just shares a static URL, defining the following function in the .m and declaring it in the .h

The next step was to add a UIButton to the main screen in the Storyboard editor to give something to share from, and right click on the button and drag from from UI Touch Up Inside event to the controller, and the didPressShare function. At this point firing up the app gave a share screen, but the actual sharing failed to return to the app - as I'd forgotten to setup the URL type as described in the documentation! This is used as the callback from the sharing process (which happens in the browser) and defaults to using the bundle name as the URL scheme. Once I registered the URL type in the Targets > Info panel, the sharing dialogue returned me to the application afterwards properly. 

Finally, I wanted some degree of confirmation that the user had shared, and the share object can use a finishedSharing call back for exactly that. I added a function to just pop up an alert when sharing had completed to the controller .m file.

To actually trigger this, we need to set the share object to have the controller as the delegate, so in the viewDidLoad function of the controller I added a line:

This sets up the share dialogue, but doesn't call the message alert. This is because return call using the URL needs to be specifically handled by the application, and control passed to the GooglePlusShare object. I needed to do a little more work in order to get the flow of control to move properly. Firstly, in the AppDelegate I needed to create a property for the share object, and setup checking the URL on open: 



And in the ViewDidLoad on the controller where I instantiate the share object, I need to share the pointer with the AppDelegate: 

This got the basic sharing happening - when the user presses the button, they're sent to plus, and can choose which circles to share to. Once done, they return to the app, which can take whatever action is needed.

Read More

Smoothing With Holt-Winter

In one of his talks at QCon, John Allspaw mentioned using Holt-Winter exponential smoothing on various monitoring instances. Wikipedia has a good entry on the subject, of course, but the basic idea is to take a noisy/spikey time series and smooth it out, so that unexpected changes will stand out even more. That’s often initially done by taking a moving average, so say averaging the last 7 days of data and using that as the current day’s value. More complicated schemes weight that average, so that the older data contributes less.

Read More

Twitter FollowingRank With Lapack

At the recent PHP UK Conference 2012 I had the opportunity to chat about machine learning and IR with a bunch of very smart people. One of the conversations included the always enlightening Rowan Merewood, and was around ranking Twitter friends. It’s reasonably well known that Google used to use a variant of PageRank based on who-follows-who to rank it’s Twitter search results (back when it had them). The question is, could the same kind of thing work over a much smaller set - say using it to rank the influence users I follow, in order, perhaps, to prioritise tweets?

Read More

Presentation Tips From Benelux

I had a great time at the recent PHP Benelux Conference in Belgium. There was a real mix of very interesting people to talk to, and I came away from it buzzing with new ideas (and a ridiculously long todo list). Some of the conversations I had during the weekend were around technical presenting at conferences and usergroups, so I thought I’d collect a handful of the tips that were discussed into a post, and use a few of my favourite speakers at the event to illustrate them.

Read More

Expecting The Unexpected With Good-Turing

A lot of interesting techniques involve taking statistical samples, and using those to predict what we’ll see in the future. Usually this works pretty well, but when we’re dealing with a lot of options or if we have some options that are very rare that approach can go pretty wrong. If we go down the street and note down how many men and women we see, we’ll probably be able to use that to predict the chance of the next person we see being male or female pretty well. However, if we were counting all the species of animals we encounter, and trying to use that to predict what we’ll see in the future, we’d likely run in to a couple of problems.

Read More

Linear Regression In PHP (part 2)

In the last post we had a simple stepping algorithm, and a gradient descent implementation, for fitting a line to a set of points with one variable and one ‘outcome’. As I mentioned though, it’s fairly straightforward to extend that to multiple variables, and even to curves, rather than just straight lines.

Read More

Benfords Law

Benfords Law is not an exciting new John Nettles based detective show, but an interesting observation about the distribution of the first digit in sets of numbers originating from various processes. It says, roughly, that in a big collection of data you should expect to see a number starting with 1 about 30% of the time, but starting with 9 only about 5% of the time. Precisely, the proportion for a given digit can be worked out as:

Read More

Monte Carlo Simulations

Monte Carlo simulations are a handy tool for looking at situations that have some aspect of uncertainty, by modelling them with a pseudo-random element and conducting a large number of trials. There isn’t a hard and fast Monte Carlo algorithm, but the process generally goes: start with a situation you wish to model, write a program to describe it that includes a random input, run that program many times, and look at the results.

Read More