Monday, July 16, 2012

Tutorial: How to use inbuilt Facebook/Twitter API in iOS6.

You need to add Social.framework in your project.
Add to your file following #import "Social/Social.h"


You can create 3 type of service:



SLServiceTypeTwitter;
SLServiceTypeFacebook;
SLServiceTypeSinaWeibo;

I have created service of Facebook.


SLComposeViewController *fbController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];


if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
        SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
        
        [fbController dismissViewControllerAnimated:YES completion:nil];
        
        switch(result){
        case SLComposeViewControllerResultCancelled:
        default:
        {
            NSLog(@"Cancelled.....");
            
        }
            break;
        case SLComposeViewControllerResultDone:
        {
            NSLog(@"Posted....");
        }
            break;
    }};
    
    [fbController addImage:[UIImage imageNamed:@"1.jpg"]];
    [fbController setInitialText:@"Check out this article."];
    [fbController addURL:[NSURL URLWithString:@"http://soulwithmobiletechnology.blogspot.com/"]];
    [fbController setCompletionHandler:completionHandler];
    [self presentViewController:fbController animated:YES completion:nil];
}



I have added image which will be posted with the message and URL on the Facebook page. Here is what the message looks like on FB.









Following are some screenshots of the app running.


If the Facebook account is not set up on device, it gives you this pop up.

SLComposeViewController with added URL and Image.

SLComposeViewController with added text,URL and image,
Also location has been set on the post
just like we can do on our Facebook page.

You can change the album in which you want to post this image.

You can choose which group you want to display this post to.





Link to download the sample code.

13 comments:

  1. No need to use __block there

    ReplyDelete
  2. IS there any way to post the photo in a nwe album, which name is given by the app?

    ReplyDelete
  3. You shouldn't use :
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])

    because on device (not simulator) it won't prompt for user to configure facebook if no account were set up.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. when i come to 1st snapshot (in above screen shots) by clicking share it , after clicking setting button or cancel button which are in first snapshot. it is simply dismissed and facebook section is also dismissed when i click any button and remains on same page.
    can anyone please tell me why it is happening ??
    Thank you

    ReplyDelete
  6. Good example thank u so much!. How to post video file to Facebook

    ReplyDelete
  7. any chance you make a similar tutorial to receive a certain facebook/twitter account live feed? would appreciate

    thanks

    ReplyDelete
  8. is there any way to post something without the pop-up compose view if you have a variable with a preloaded message?

    Perhaps just have a button that posts to the respective FB account and only have a alert box that saids your message has been posted.

    ReplyDelete
  9. is there any way to post something without the pop-up compose view if you have a variable with a preloaded message?

    Perhaps just have a button that posts to the respective FB account and only have a alert box that saids your message has been posted.

    ReplyDelete
  10. image is not getting upload for me ,but all other url and all getting shared

    ReplyDelete
  11. in your samplecode also image is not getting upload

    ReplyDelete
  12. Has anyone been able to allow posting image to a Group Album? Albums show up when 'my page' is selected, but if Friends or Groups are selected then have no option to select an album.

    ReplyDelete