Sunday, 16 September 2012

Facebook Twitter Weibo ios6 integration

1. Create/Open an existing xcode project

2. Add the 'Social Framework' as shown in the image below. After adding the framework, make it optional to make the app compatible with older versions of ios.






3. Create an interface with a button 'Post to FB'. We shall invoke the facebook api upon clicking this button.


4. Create an IBAction and associate the button event with this action

-(IBAction) postToFB:(id)sender;
5.  Import the social framework

#import "social/Social.h"

6. Add the following code to the IBAction method created in the previous step:
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){ if (result == SLComposeViewControllerResultCancelled) { NSLog(@"Post cancelled"); } else { NSLog(@"Post published"); } [controller dismissViewControllerAnimated:YES completion:Nil]; }; controller.completionHandler =myBlock; [controller setInitialText:@"This is a sample"]; [controller addImage:[UIImage imageNamed:@"myimage.png"]]; [self presentViewController:controller animated:YES completion:Nil]; } else { NSLog(@"service not available"); }

7. Compile and run the application

8. Press the "Post to FB" button and a FB popup will appear (the simulator will give this warning)


  9. To integrate with Twitter, replace the 'SLServiceTypeFacebook' with 'SLServiceTypeTwitter' in the code. With iOS6, SLComposeViewController is a generic view controller that supports Facebook, Twitter and Weibo social integration. The only parameter to be passed to the view controller is the service we wish to integrate:
  • SLServiceTypeFacebook
  • SLServiceTypeTwitter
  • SLServiceTypeSinaWeibo

No comments:

Post a Comment