Facebook Tutorial for iOS: How To Use Facebook’s New Graph API from your iPhone App
A Facebook tutorial in which you will learn how to use Facebook’s Graph API from your iPhone app to authenticate the user, post to their wall, and more. By Ray Wenderlich.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Facebook Tutorial for iOS: How To Use Facebook’s New Graph API from your iPhone App
20 mins
Calling the Login Dialog
Next add the implementation for when the login button is tapped:
- (IBAction)loginButtonTapped:(id)sender {
NSString *appId = @"8fa673a06891cac667e55d690e27ecbb";
NSString *permissions = @"publish_stream";
if (_loginDialog == nil) {
self.loginDialog = [[[FBFunLoginDialog alloc] initWithAppId:appId
requestedPermissions:permissions delegate:self] autorelease];
self.loginDialogView = _loginDialog.view;
}
if (_loginState == LoginStateStartup || _loginState == LoginStateLoggedOut) {
_loginState = LoginStateLoggingIn;
[_loginDialog login];
} else if (_loginState == LoginStateLoggedIn) {
_loginState = LoginStateLoggedOut;
[_loginDialog logout];
}
[self refresh];
}
First up is our app id, which you get from the Facebook developer’s portal when you create your app. The id in the code is the same ID from the “My Grades” test app we made in the Facebook Connect tutorial. Feel free to use this, or replace it with your own.
Next up we set what extended permissions we want. You can read up on the extended permissions that are available, but for now just know that this is the one you use if you want to post to a user’s wall, or upload photos.
Next we check to see if the login dialog has been created, and if not create it passing in our app id and permissions. Note we also store a reference to the login dialog view, which is retained. You might say to your self: WTFBBQ?
Well, since the view controller is in the background, usually its viewDidLoad will not be called until it is displayed (because that is usually the first time the views are actually needed). This is not good for us, because we want the view (and hence the web view) to be loaded even if it isn’t being displayed, so that it can do some work in the background.
So to trigger that to happen, we just store a reference to the view. Don’t know if there is a better/more elegant way to do that, but this is one way that works.
Next, we check our login state, and either call login or logout on the dialog. We also update our state accordingly, and refresh our display.
Implementing FBFunLoginDialogDelegate
A couple more functions to add and then we’re done: the implementation of the FBFunLoginDialogDelegate!
- (void)accessTokenFound:(NSString *)accessToken {
NSLog(@"Access token found: %@", accessToken);
_loginState = LoginStateLoggedIn;
[self dismissModalViewControllerAnimated:YES];
[self refresh];
}
- (void)displayRequired {
[self presentModalViewController:_loginDialog animated:YES];
}
- (void)closeTapped {
[self dismissModalViewControllerAnimated:YES];
_loginState = LoginStateLoggedOut;
[_loginDialog logout];
[self refresh];
}
In accessTokenFound, all we do is print out the access token and dismiss the dialog. Now that we have the access token, we could do all kind of fun things, but this blog post has gone on more than long enough already so we’ll save that for next time :]
In displayRequired, we just present the login dialog so the user can enter their username and password, and in closeDialog we just shut things down.
And that’s it! Compile and run the app, and you should be able to log in and out of Facebook:
You can also see that it will automatically log you in if you log in, exit the app, restart and log in again, since the cookies will have been saved for your session. However – it appears that the web view must save the cookies periodically in the background, so you’ll have to wait a minute or so after you log in for the cookies to be persisted to disk for it to work.
Where To Go From Here?
Here is a sample project with all of the code we developed in the above Facebook tutorial.
Now that we have a login token, check out the next Facebook tutorial in the series, where we’ll cover how you can use it to get all kinds of interesting data from Facebook – including whether you prefer babes or dudes!
Please comment below if you use or are planning on using the new Facebook Graph API in your Facebook apps – or if you’re just planning on sticking with Facebook Connect for now!