How to post to Facebook fan pages with Graph API and iOS SDK
Posted by Leonard Ah Kun in Facebook, How-to, iOS
I’ve have recently taken a look at the Facebook’s Graph API and iOS SDK. Facebook has made it relatively simple to add Facebook features in your iOS app. One particular problem I came across was posting items to a fan page as the admin. I also struggled to find any good documentation/tutorials using the iOS SDK specifically. Anyway. here’s how you do simple posts to a fan page using the Facebook iOS SDK.
Download the iOS SDK here. Follow the instructions how to add it to your project. It is really as simple as dragging the folder into your project.
You will also need to create a Facebook application, which will request a users’ various permissions you wish to use to interact with Facebook. Once an app is created you will get an APP ID which you need to use in your project.
An overview of the Graph API can be found here. For posting to a fan page, we are interested in using page impersonation. You are required to add the “manage_pages” permission when authenticating the user.
Facebook *fb = [[Facebook alloc] init];
self.facebook = fb;
[fb release];
NSArray *permissions = [NSArray arrayWithObjects:@"read_stream",
@"offline_access",
@"publish_stream",
@"manage_pages"
,nil];
[facebook authorize:FB_APP_ID permissions:permissions delegate:self];
Once authenticated, your Facebook object will be populated with an access_token. The current access token can be used to request your own personal data, and post items to your own facebook page. With the current access_token, the following code will post a new status to your wall.
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"hello world!",
@"message", nil];
[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod: @"POST"
andDelegate:self];
To post to your fan page, you need to make a call to me/accounts.
[facebook requestWithGraphPath:@"/me/accounts" andDelegate:self];
This will return a JSON string which looks like this
{
{
"access_token" = "XXXXXXXXXXXXXXXXX";
category = "Local business";
id = 163890540313824;
name = "Cobi Labs";
},
{
"access_token" = "XXXXXXXXXXXXXXXXX";
category = "Local_business";
id = 273912855170;
name = "Cobi Interactive";
}
}
These items are all the pages/applications you own. The result variable in the - (void)request:(FBRequest *)request didLoad:(id)result delegate method will contain an array of NSDictionary’s. You will need to parse through these items to find your fan page you want to post to. Copy the access_token associated with the desired page. Setting your Facebook objects access_token variable to the copied access_token, will now give you access to your fan page.
facebook.accessToken = new_access_token;
Now if you call
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"hello world!",
@"message",
nil];
[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST"
andDelegate:self];
your fan page will have a status update as your fan page.



hello
this is a good example thx
but i want to post to a fan page which is not mine…
how can i do that please
i have post here for more informations :
http://stackoverflow.com/questions/5378319/help-accessing-a-fan-page-with-facebook-sdk-for-ios
thx for your help