Search This Blog

Wednesday, 2 November 2011

Facebook Integration in your application



Download FacebookSDK and copy in your project.


Now import facebook headers file in your .h file


#import "Facebook.h"
#import "FBConnect.h"
#import "FBRequest.h"

//Add these four delegate in .h file
<FBSessionDelegate,FBDialogDelegate,FBLoginDialogDelegate,FBRequestDelegate>

like:
@interface FacebookViewController : UIViewController <FBSessionDelegate,FBDialogDelegate,FBLoginDialogDelegate,FBRequestDelegate>{

//Now declare variables in .h file
Facebook *facebook;
NSArray *permissionsArray;

Now come to .m file and add this on the top after @implementaton
static NSString* kAppId = @"227448770601908";

//Now copy and paste from facebook start to facebook end

///facebook start
#pragma mark -
#pragma mark Facebook delegate methods

-(void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response{
NSLog(@"Response : %@", [[response URL] absoluteString]);
}

-(void)request:(FBRequest *)request didLoad:(id) result {
NSLog(@"result:%@",result);
NSLog(@"result:%@",[result objectForKey:@"name"]);
}

- (void)request:(FBRequest *)request didFailWithError:(NSError *) error {
NSLog(@"result:%@",[error description]);
}

-(void)fbDidLogin {
NSString *publishString = [NSString stringWithFormat:@"Blogs"];
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"Photo Rating",@"text",@"http://iphone-app-dev-beginners.blogspot.com/",@"href", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];

NSDictionary* imageShare = [NSDictionary dictionaryWithObjectsAndKeys:
@"image", @"type",
@"http://iphone-app-dev-beginners.blogspot.com/", @"src",
@"http://iphone-app-dev-beginners.blogspot.com/", @"href",
nil];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
@"PottyLegs for iPhone!", @"name",
@"http://iphone-app-dev-beginners.blogspot.com/", @"href",
@"string",@"description",
[NSArray arrayWithObjects:imageShare, nil ], @"media",
nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
//    NSLog(attachmentStr);
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  kAppId, @"api_key",
  @"Share on Facebook"@"user_message_prompt",
  actionLinksStr, @"action_links",
  publishString, @"message",
  attachmentStr, @"attachment",
  nil];
[facebook dialog: @"stream.publish"
  andParams: params
andDelegate:self];
}

//// data receive from server end

-(void)fbDidNotLogin:(BOOL)cancelled {
NSLog(@"did not login");
}

- (void)fbDidLogout {
}

//facebook end

//call facebook function in your Button action
-(IBAction)FacebookButtonPressed
{
facebook = [[Facebook alloc] initWithAppId:kAppId];
[facebook authorize:permissionsArray delegate:self];
}



2 comments:

  1. Facebook Integration is playing brilliant role in all this thing.

    ReplyDelete
  2. Thanks Allison , and any i can share with you it will be pleasure for me

    ReplyDelete