Pages

Subscribe:

Thursday, March 24, 2011

iPhone SDK - Post messages in Facebook

How to post messages or comments in Facebook wall with iPhone SDK? Here is the sample code!

1. Download the Facebook API from Git
2. Start a new project in xcode.
3. Drag and drop the src folder from Facebook API to our new project (in classes)

Here is your app delegate interface.

App Delegate .h


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


@class FacebookSampleViewController;

@interface FacebookSampleAppDelegate : NSObject <UIApplicationDelegate, FBSessionDelegate> {
    UIWindow *window;
    FacebookSampleViewController *viewController;

Facebook *facebook;
}

@property (nonatomic, retain) Facebook *facebook;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet FacebookSampleViewController *viewController;

@end

Here is the app delegate implementation.
You need the APPID from Facebook here. publish_stream permission is to post messages on your wall.

App Delegate .m



#import "FacebookSampleAppDelegate.h"
#import "FacebookSampleViewController.h"

@implementation FacebookSampleAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize facebook;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.


facebook = [[Facebook alloc] initWithAppId:@"xxxxxxxxxxxxx"]; // Use your app id here
NSArray* permissions =  [[NSArray arrayWithObjects:@"publish_stream", @"read_stream", nil] retain];
[facebook authorize:permissions delegate:self];
    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [facebook handleOpenURL:url]; 
}


Now, find the view controller interface below. Here we have a button which call the post messages action/dialog.

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

@class FacebookSampleAppDelegate;

@interface FacebookSampleViewController : UIViewController <FBRequestDelegate>{

FacebookSampleAppDelegate *facebookSampleAppDelegate;
}
@property (nonatomic, retain) FacebookSampleAppDelegate *facebookSampleAppDelegate;

- (IBAction) showPost;

@end


And here is the implementation for view controller.

#import "FacebookSampleViewController.h"

@implementation FacebookSampleViewController

@synthesize facebookSampleAppDelegate;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

facebookSampleAppDelegate = (FacebookSampleAppDelegate *)[[UIApplication sharedApplication] delegate];

//[facebookSampleAppDelegate.facebook authorize:nil delegate:self];
    [super viewDidLoad];
}



- (IBAction) showPost {
[facebookSampleAppDelegate.facebook dialog:@"feed" andDelegate:self];
}



Run the above application.
It will ask you to authorize Facebook connect.
Then click the post button. You will be presented the post messages dialog through which you can publish messages on your wall.

1 comments:

Unknown said...

Your post gives insight to more facebook users. Thanks for sharing this information regarding facebook application.

Post a Comment