I recently added Facebook integration to one of my Apps. It's taken me over 2 years to do this. Why have I been putting it off? I'd always chalk it up to either "I don't have time," or "it's going to be a nightmare."
Little did I know that I was wrong on both counts.
A Slight Diversion
First, why would you want to add Facebook integration (besides that "everyone else is")? Well, your users probably want it, they
want to share things with their friends, and you don't really want them leaving your app to go do something as simple as posting a message
to their wall or uploading a photo, right? That's a great reason. Right?
Well, sure, why not. But you are in the app business, and the more people that hear about your app, the better. Whenever a user uses
your app to post to their wall, upload a photo, check into a place, or anything, there is a nice little link to your app's Facebook page,
which then of course links to your actual app, on the App Store.
When one of these posts show up in someone's news feed, it almost feels like a recommendation. Not quite, but almost. Their friend is using
some software, so there must be some merit.
Facebook is free advertising. And not only is it free, but your loyal users are the ones that will be spreading the word by
simply using the feature that they desperately want.
Back to the SDK
Now, on to the nuts and bolts of it all.
First off, you should definitely be checking out the Facebook developer documentation. The
documentation is like most documentation...meaning it gives you just enough to get an idea of how maybe it works, but nowhere near enough to
actually implement it. But there are a few sections that should help with the concepts, such as
Graph API Key concepts and this Mobile Applications page.
You'll need to create a Facebook Application for your app (this also creates a regular page for your app within FB, sort of like the old Fan
pages). Just click the "Set Up New App" button on the top right of this page. There are a few
key strings that Facebook generates to uniquely identify your app, but mostly you just need the Application ID for integrating with iOS.
Next, download the latest Facebook SDK for iOS and drag the FBConnect folder
into your XCode project. Be sure to read up on all the info just below the download section. There's a lot of great information there.
Logging into Facebook
Now, I'm going to simplify a lot this code for the blog's sake, but it should be enough info to get you logged into Facebook via your app. I'll expand on the practical side of this in another post.
To log into Facebook, you set some object (say, a UIViewController) to be a FBSessionDelegate. Then to actually log in, all you need is:
- (void) init
{
// myFacebookClient is a member var of type Facebook *
myFacebookClient = [[Facebook alloc] initWithAppId:YOUR_FACEBOOK_APPLICATION_ID];
}
- (IBAction) logIntoFacebook:(id)sender
{
// obviously hook this up to a button in your UI
// authorize: takes an array of strings that is the requested permissions.
// we want to publish to their stream. If you pass nil, you can just get
// some basic, read-only information.
[myFacebookClient authorize:[NSArray arrayWithObject:@"publish_stream"] delegate:self];
}
// FBSessionDelegate methods
- (void) fbDidLogin
{
// login succeeded
}
- (void) fbDidNotLogin:(BOOL)cancelled
{
// login failed
}
- (void) fbDidLogout
{
// user logged out
}
Simple right? Yes, but there is one more step to actually log in, even though just this code will work on some older devices.
See, the cool thing is that Facebook has developed the SDK to work in several iOS situations and chooses the best login procedure
based on the device capabilities. The "old" FBConnect way of logging into Facebook was to prompt the user in the app with a
UIView that required them to enter their username & password. But so many people use Facebook, and so many even have the Facebook App installed,
that they take advantage of that.
So, in the situation where the user has the Facebook app installed and can multitask/fast-app-switch, they simply
call up the FB app (via their custom URL scheme) and use the user's credentials that are *already* entered to log them into FB. So the only
thing they are prompted for is to allow your app to access their information (according to the access you requested in the authorize: call).
If the user doesn't have FB installed, but can still fast-app-switch, they switch to safari and do the login from there. Finally, if none
of those situations arise, they just use the old UIView dialog method.
However, to USE the fast-app-switch method, you must add a few extra bits. First, in your AppDelegate:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [self.viewController.myFacebookClient handleOpenURL:url];
}
Then, add the following to your info.plist:
So via the magic of iOS URL schemes, the FB app is going to be called up when the user needs to login, then when the finish, the FB app
(or even the web page, will call up your app via a URL scheme of the format "fb". When your app gets
launched (or foregrounded) via the URL, you pass on the URL to the Facebook client object, and it completes the login (calling the fbDidLogin
method). Quite clever really. Of course, if you're already using another URL scheme for your app, you'd have to check the scheme in the application:handlOpenURL:
method and do the right thing.
If you've come this far, you should be able to put a breakpoint into your fbDidLogin: method in your delegate and see it stop there. That's the
first big step, from here it gets mostly easier!
Next time
Look for Part 2 of this post, where we figured out just how easy it was to retreive data about the user, how to post things to their wall, and even
how to upload photos to their albums.