iPhone Facebook Connect: How Do I publish feed with FacebookAgent
I planned to write a series of “How Do I…”s with Facebook Connect. Certainly the primary target is YOU but it will also work as a reference to me as well
The first article will be on publishing a feed.
So lets start…!
App Requirement Spec
- Option to login/logout to Facebook
- Option to publish a feed with:
- With an image with link
- Action link
Get FacebookAgent code
Checkout the latest code from Google Code project. The checkout url is:
svn checkout http://iphone-facebook-twitter-connect-easy-integration-tool.googlecode.com/svn/trunk/ iphone-facebook-twitter-connect-easy-integration-tool-read-only
Create a project
Open up the xcode project and create a view based application; name it “FacebookFeedPublish”
Add FacebookAgent code to project
Right click on the project. Select Add->Existing File. Select the “xutils” folder from the checked out code.

Do not copy.
Add JSON libraries
Right click on the project. Select Add->Existing File. Select the “JSON” folder from the checked out xutils-example project.

Do not copy this either.
Build the project. It should build with no error.
Initialize FacebookAgent
Edit FacebookPublishFeedAppDelegate.m.
import FacebookAgent.h at the top.
#import FacebookAgent.h
Add the following line in applicationDidFinishLaunching method:
// initiazlie FacebookAgent [[FacebookAgent sharedAgent] initializeWithApiKey:@"YOUR_API_KEY" ApiSecret:@"YOUR_API_SECRET" ApiProxy:nil];
You have to put your Facebook Application api key and secret. Just incase you are not sure about this, create a Facebook application from http://www.facebook.com/developers/createapp.php. After creating one, you will get api key and secret. Copy and paste those value in the above code.
Prepare FacebookConnectDelegate handler
We can use FacebookAgent in multiple view controllers. Very likely we will use it in many places. We have to override and define many delegate methods in each view controllers where we like to use FacebookAgent.
To simplify the process, we well create a UIViewController subclass which will define all the FacebookAgentDelegate methods. Our view controller class will extend this class and override only the required ones.
Add a new file of UIViewController subclass. Name it MyCustomController.
Uncheck “With xib for user interface”.
Implement FacebookAgentDelegate protocol:
#import "FacebookAgent.h" @interface MyCustomController : UIViewController{ }
Now define the protocol methods:
#pragma mark FacebookAgentDelegate
- (void) facebookAgent:(FacebookAgent*)agent statusChanged:(BOOL) success{}
- (void) facebookAgent:(FacebookAgent*)agent didLoadInfo:(NSDictionary*) info{}
- (void) facebookAgent:(FacebookAgent*)agent didLoadFriendList:(NSArray*) data onlyAppUsers:(BOOL)yesOrNo{}
- (void) facebookAgent:(FacebookAgent*)agent didLoadPermissions:(NSArray*) data{}
- (void) facebookAgent:(FacebookAgent*)agent didLoadFQL:(NSArray*) data{}
- (void) facebookAgent:(FacebookAgent*)agent permissionGranted:(FacebookAgentPermission)permission{}
- (void) facebookAgent:(FacebookAgent*)agent photoUploaded:(NSString*) pid{}
- (void) facebookAgent:(FacebookAgent*)agent requestFaild:(NSString*) message{}
- (void) facebookAgent:(FacebookAgent*)agent loginStatus:(BOOL) loggedIn{}
- (void) facebookAgent:(FacebookAgent*)agent dialog:(FBDialog*)dialog didFailWithError:(NSError*)error{}
Then edit FacebookPublishFeedViewController.h file and make it extend to the newly created class:
Build the project, it should build without any error.
Enable login/logout option
Add a button to the view. Set its image for default state “Connect_iphone.png”.
Press ‘cmd + =’ to adjust the size.

Set selected state button “Logout_iphone.png”.

These images are downloaded from Facebook site and available in FacebookAgent code base.
Connecting the button
Edit FacebookPublishFeedViewController class.
Create an IBOutlet connect it with the button.
Then create an IBAction method and set it as button onTouchUpInside handler.
#import#import "MyCustomController.h" @interface FacebookPublishFeedViewController : MyCustomController { UIButton* btnFbConnect; } - (IBAction)OnFBConnect:(id)sender; @property(nonatomic, retain) IBOutlet UIButton* btnFbConnect; @end
In OnFBConnect method we will login to facebook. If already connected we will sign out. So, method definition will be like this:
#pragma mark actions
- (IBAction)OnFBConnect:(id)sender{
if([[FacebookAgent sharedAgent] isLoggedIn]){
[[FacebookAgent sharedAgent] logout];
}else {
[[FacebookAgent sharedAgent] login];
}
}
Update connect button state
Override the following method like below in FacebookPublishFeedViewController class:
#pragma mark FacebookAgentDelegate method
- (void) facebookAgent:(FacebookAgent*)agent loginStatus:(BOOL) loggedIn{
if(loggedIn){
btnFbConnect.selected = YES;
}else {
btnFbConnect.selected = NO;
}
}
This code will update the button image according to login status.
Run the app and connect to Facebook by clicking the button…. Oops button state is not changed! Add the following line in MyCustomController class viewLoaded method:
- (void)viewDidLoad {
[super viewDidLoad];
[FacebookAgent sharedAgent].delegate = self;
}
Now build and run… after login or logout now button image is updated accordingly.
Publish Feed
Add another Round button and add touch up inside handler in FacebookPublishFeedViewController class in this way:
- (IBAction)OnFBPublishFeed:(id)sender{
[[FacebookAgent sharedAgent] publishFeedWithName:@"Hello Facebook!"
captionText:@"This is a test publish"
imageurl:@"http://xapplab.com/images/appss/puzzleshot/puzzleshot-icon.png"
linkurl:@"http://itunes.apple.com/app/puzzleshot/id343659310?mt=8"
userMessagePrompt:@"Whats on your mind?"
actionLabel:@"Play PuzzleShot"
actionText:@"PuzzleShot!"
actionLink:@"http://itunes.apple.com/app/puzzleshot/id343659310?mt=8"];
}
The feed will look this:

Thats it!
Next article will be on showing logged in user information and image. But let me manage some time first
Download the project source :FacebookPublishFeed.zip
Don’t forget to change api key and secret to run the project correctly!
-
TinyTechnician
-
Jon
-
Ali
-
coneybeare
-
coneybeare
-
Irene
-
rajdn
-
rajdn
-
Shaikh Sonny Aman
-
rajdn
-
Shaikh Sonny Aman
-
JWeldin




(4.60 out of 5)