Monday, 18 June 2012

Steps to integrate Facebook in iOS6



Apple unveiled his iOS6 beta version. It has a great impact on FB user, and obviously to the developers. Tried my hand to integrate this in my app and sharing my experience. I think this will help many people a lot.





1. In order use the Facebook we need to add the Facebook framework to our project. Select your project in the project navigator and then the project target. Go to the Build Phases tab and click on the + button inside the Link Binary With Libraries section, on the window that appears navigate to the Social.framework file and click Add.

2.  Open the controller file and add :
                      #import "Social/Social.h"

These two steps are necessary to integrate Facebook. Apart from these we need to set Facebook credentials in the settings on iPhone initially.

After that we need to write code for post on wall.

1.     POST TEXT:

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
  
        SLComposeViewController *faceBookSheet=[[SLComposeViewController alloc] init];
        // Sets viewcontroller to FB type
        faceBookSheet=[SLComposeViewController composeViewControllerForServiceType: SLServiceTypeFacebook];

        //Calls the function for set Text
        [faceBookSheet setInitialText:@"Initial Text from iOS 6 facebook Demo"];
       
        // Specifying a block to be called when the user is finished. This block is not guaranteed
        // To be called on any particular thread. It is cleared after being called.
        [faceBookSheet setCompletionHandler:[self setComplitionHandlerFunction]];
   
        //Presenting the FB sheet
        [self presentViewController:faceBookSheet animated: YES completion: nil];
    
// presentModalViewController is deprecated in iOS 6.
   
        [faceBookSheet release],faceBookSheet=nil; }
A special function is required which handles the event when user is finished.

[faceBookSheet setCompletionHandler:[self setComplitionHandlerFunction]];

Inside the setComplitionHandlerFunction:

SLComposeViewControllerCompletionHandler resultFB = ^(SLComposeViewControllerResult result) {
        NSString *output;
       
        switch (result) {
            case SLComposeViewControllerResultCancelled:
                output = @"ACtionCancelled";
                break;
            case SLComposeViewControllerResultDone:
                output = @"Post Successfull";
                break;
            default:
                break;
        }
      
       
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook Complition Message"
                                                        message:output
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
       
        [alert show];
        [alert release];
    };
   
    return resultFB;


2.     POST IMAGE:

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *faceBookSheet=[[SLComposeViewController alloc]init];
       
        faceBookSheet=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [faceBookSheet addImage:[UIImage imageNamed:@"HeroFish.png"] ];
       
       
        [faceBookSheet setCompletionHandler:[self setComplitionHandlerFunction]];

       
        [self presentViewController:faceBookSheet animated:YES completion:nil];
        [faceBookSheet release],faceBookSheet=nil;       
    }



3.     POST URL:

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *faceBookSheet=[[SLComposeViewController alloc]init];
        faceBookSheet=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        NSURL *url=[NSURL URLWithString:@"http://www.leewayhertz.com/about.php"];
        [faceBookSheet addURL:url];
        [faceBookSheet setCompletionHandler:[self setComplitionHandlerFunction]];

       
        [self presentViewController:faceBookSheet animated:YES completion:nil];
        [faceBookSheet release],faceBookSheet=nil;       
    }

No comments:

Post a Comment