Friday, August 24, 2012

Integrating Facebook in iOS 6


Integrating Facebook in iOS 6
Integrating Facebook into your app is a great way to users can share content from your app with their friends. In iOS 6 apple integrated a new framework called Social.framework. In this social.framework include the twitter, Facebook, SinaWeibo.
These are the types available for communicating with the social.framework

SLServiceTypeFacebook,
SLServiceTypeTwitter,
SLServiceTypeSinaWeibo 
In this tutorial i am going to explain about how we can work with Facebook in iOS 6.  Create a single view based application and named it as Facebook. add one UIButton to the UIView and rename it as Facebook. Next we need to ad the Social.framework to the project. After adding the Social.framework to the project we need to add the Accounts.framework to the project. you can use the same steps to add the Accounts.framework to the project.
facebook ios 6 integration


Now we need to include the headers to the view controller class . For working with the Facebook i am going to import the two headers in the view controller.m file
#import "social /Social.h"
#import "accounts /Accounts.h"
next we need to create a method to work with Facebook:-
 -(IBAction)faceBook:(id)sender;
Now we need to implement the faceBook method in the implementation .
-(IBAction)faceBook:(id)sender{
 
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
 
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
 
        ACAccount *account = [[ACAccount alloc] initWithAccountType:accountType];
        NSLog(@"%@, %@", account.username, account.description);
    }];
 
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
 
        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
 
        SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
            if (result == SLComposeViewControllerResultCancelled) {
 
                NSLog(@"Cancelled");
 
            } else
 
            {
                NSLog(@"Done");
            }
 
            [controller dismissViewControllerAnimated:YES completion:Nil];
        };
        controller.completionHandler =myBlock;
 
        [controller setInitialText:@"Test Post from mobile.safilsunny.com"];
        [controller addURL:[NSURL URLWithString:@"http://www.mobile.safilsunny.com"]];
        [controller addImage:[UIImage imageNamed:@"fb.png"]];
 
        [self presentViewController:controller animated:YES completion:Nil];
 
    }
    else{
        NSLog(@"UnAvailable");
    }
 
}
wire the method with the UIButton in the XIB. Now Run the app and test it.
facebook ios 6













 




 

 


That’s it. you are done with the Facebook. 



1 comment:

  1. This has not been successful for me running Xcode 4.5 with 6.0

    ReplyDelete