Monday, July 16, 2012

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=[SLComposeViewControllercomposeViewControllerForServiceType:SLServiceTypeFacebook];


if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
        SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResultresult){
        
        [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.

No comments:

Post a Comment