Well, it had to happen sometime. I’ve released my first bit of iOS open source. Actually the code has been out a while, but the blog was taking a Gatorade break on the sidelines and autographing fan photos.

As you may have heard, Facebook finally had it’s IPO last week. You’re all shorting FB, right? Because apparently the IPO was seriously screwed up, with all manner of botched trades, road show re-valuations, and unnecessary additional shares.

Well, you’ll be pleased to know that their IPO isn’t the only shoddy bit of work to come out of the company. Facebook also has this iOS SDK for connecting your app with a user’s Facebook data. And it is just awful.

Enter today’s delicious bit of open source, SPFacebookClient. SPFacebookClient is a lightweight wrapper around the Facebook iOS SDK. The wrapper includes a shared singleton client object which manages authentication and session data and which provides access to the API using blocks based methods. All you need to do is set your application identifier and permissions in a custom subclass and the rest is a snap.

For example, you can now log in with a single line of code:

[[SPExampleFacebookClient sharedClient] login:^(BOOL success, id result, NSError *error) {
    NSLog(@"%i,%@,%@",success,result,error);
}];

Instead of, say, six different methods and delegate callbacks with object level state management. I mean, what the hell were those guys thinking?

Likewise, it’s stupid easy to make calls to the Graph API:

[[SPExampleFacebookClient sharedClient] requestWithPath:@"me/friends" completionHandler:^(BOOL success, id result, NSError *error) {
    NSLog(@"%i,%@,%@",success,result,error);
}];

Or to make FQL Queries:

[[SPExampleFacebookClient sharedClient] requestWithQuery:@"SELECT uid, name, pic FROM user WHERE uid=me()" completionHandler:^(BOOL success, id result, NSError *error) {
    NSLog(@"%i,%@,%@",success,result,error);
}];

I know. Awesome, right? Blocks make everything better.

Well, there you have it. A handy wrapper around an API to one of the most popular services on the planet. You might be wasting money on Facebook’s IPO, but at least you won’t be wasting time on their SDK. Or money, because SPFacebookClient is available for the low, low price of absolutely nothing thanks to a BSD License.

SPFacebookClient is for the iOS. I’ve tested it on 5.0 and 5.1. The code is available for download at Github.

Leave a Reply