In iOS 6 you can easily integrate Facebook in your app. There are two ways to do this. One way is to use the native ‘share sheet’ (SLComposeViewController) which doesn’t require the app to request access to the Facebook account of the user. It simply shows the SLComposeViewController and processes the request.

However if you want more control and options you’ll need access to the Facebook account and create SLRequests. This is pretty easy!

Create Facebook app

First of all create an app on Facebook’s developer section. Make sure you enter the correct bundle identifier otherwise it won’t work.

Request Access to Account

You need to provide a token if you want to use authorised API methods (like posting to Wall). Fortunately iOS does this for us. We only need to request access to the account of the user. It’s pretty easy, especially if you have experience with Twitter in iOS 5. There’s one difference however; users can only have one Facebook account (in contrast to Twitter where you can have multiple accounts).

ACAccountType *facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

// We will pass this dictionary in the next method. It should contain your Facebook App ID key, 
// permissions and (optionally) the ACFacebookAudienceKey
NSDictionary *options = @{ACFacebookAppIdKey : @"<your key>",
                          ACFacebookPermissionsKey : @[@"email", @"publish_stream"],
                          ACFacebookAudienceKey:ACFacebookAudienceFriends};
 
// Request access to the Facebook account. 
// The user will see an alert view when you perform this method.
[_accountStore requestAccessToAccountsWithType:facebookAccountType
                                       options:options
                                    completion:^(BOOL granted, NSError *error) {
    if (granted)
    {
        // At this point we can assume that we have access to the Facebook account
        NSArray *accounts = [_accountStore accountsWithAccountType:facebookAccountType];
        
        // Optionally save the account
        [_accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil];
    }
    else
    {
        NSLog(@"Failed to grant access\n%@", error);
    }
}];

With the first two BETA seeds you were required to add the options dictionary to your Info.plist, this is not required anymore. If you get an Error 7 you should check out this thread.


Post on user’s Facebook wall

We can now post a message to the wall of the user. Posting on Facebook requires a little bit more work. Consult the documentation for the post method.

// First obtain the Facebook account from the ACAccountStore
ACAccountType *accountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray *accounts = [_accountStore accountsWithAccountType:accountType];

// If we don't have access to users Facebook account, the account store will return an empty array. 
if (accounts.count == 0)
    return;
    
// Since there's only one Facebook account, grab the last object
ACAccount *account = [accounts lastObject];

// Create the parameters dictionary and the URL (!use HTTPS!)
NSDictionary *parameters = @{@"message" : message, @"link": @"http://www.apple.com"};
NSURL *URL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

// Create request
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
                                        requestMethod:SLRequestMethodPOST 
                                                  URL:URL 
                                           parameters:parameters];

// Since we are performing a method that requires authorization we can simply
// add the ACAccount to the SLRequest
[request setAccount:account];

// Perform request
[request performRequestWithHandler:^(NSData *respData, NSHTTPURLResponse *urlResp, NSError *error) {
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:respData 
                                                                       options:kNilOptions 
                                                                         error:&error];
    
    // Check for errors in the responseDictionary
}];

It’s pretty simple just don’t forget to check the responseDictionary for any errors (even though the error of the request itself could be nil). The dictionary will contain an error object if an error has occurred.


Logging out

It seems that there is no way to log the user out (even tough there is a method called removeAccount:withCompletionHandler:). The users need to do this manually by revoking app access in Settings.app.