Wednesday 15 May 2013

How to post on Facebook wall from your iOS application.


I personally searched many blogs and website regarding this topic, but unfortunately I could not find any satisfactory code for this. Well somehow I managed to integrate Facebook in my iOS application and able to post on my Facebook wall. So I decided to write a post regarding this.

As we know, Facebook now launched new graph APIs (I will discus in the next post) for iOS-6 and later. These APIs are easy to use but the problem is that these doesn't support lower iOS versions. So in this post I will tell how to integrate Facebook using old Facebook SDK. I think old Facerebook SDK's UI presentation was good ?
  
Resources
  • Download source code here
  • Download Facebook old SDK (FBConnect) here.
  • Download (If required) JSON folder here.
Let's start step by step.

Step 1:
Register your application with Facebook. Go to Facebook developer portal to register your application.After successful registration you will get your Facebook app. Id, this Id you need to use in your application.

Step 2:
Only for an example I am creating a single view Xcode project, just to show you the integration process. In the same way you can integrate in any application.

Step 3:
Now just drag and drop the FBConnect folder into your project (FacebookTest in our example). Select copy items into destination group's option (Refer below images).



Same way add the JSON folder to your project too.

Step 4:
Now import the "FBConnect.h" file in your header file where you want to use it. Create a Facebook member object and and include few FB Delegates. Your header file will look like below.

#import <UIKit/UIKit.h>
#import "FBConnect.h"

@interface ViewController : UIViewController<FBSessionDelegate, 
FBRequestDelegate,FBDialogDelegate, FBLoginDialogDelegate> { 
Facebook    *facebook;
}
@end

Step 5:
Now in your implementation (.m) file include the below code. Here "onPostMessage" method  is my button action.


#import "ViewController.h"


static NSString* faceBookAppId = @"Your Facebook App Id";

-(IBAction)onPostMessage:(id)sender {

   if(nil == facebook) {

        facebook =[[Facebookalloc]initWithAppId:
                        faceBookAppId andDelegate:self];

        facebook.sessionDelegate = self;

    }

NSMutableDictionary *params =   [NSMutableDictionarydictionaryWithObjectsAndKeys:

          // @"This is what I say about myApp:",@"message",//user msg

             @"MyApp!", @"name",  //Bold/blue name

             @"MyApp is cool!", @"caption"//1st line

             @"This is a word game", @"description"// 2nd line
             @"https://itunes.apple.com/us/app/hellowords-hd/id545092692?mt=8", @"link", //App Link
             @"http://farm6.static.flickr.com/5230/5576336654_075c2abcf9_s.jpg", @"picture", //Image or logo link

             @"", @"properties",nil];

    [facebook dialog:@"feed" andParams:params andDelegate:self];
}


#pragma mark -----------FBSessionDelegate

- (void)fbDidLogin {

    //    //to Update the Status
}


#pragma mark ------- FBDilog delegate.

- (void)dialogDidComplete:(FBDialog *)dialog {

    UIAlertView* alertView = [[UIAlertView alloc] 
       initWithTitle:@"" message:@"MessagePosted" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];

    [alertView show];

    [alertView release];

 
Step 6:
Now run the application, be sure that you have internet.


Press the Post Link button to post on your Facebook wall.

Please leave your comments.
Follow me +on Twitter


No comments:

Post a Comment