Tuesday 5 February 2013

iOS 6 Social Framework

Prior to iOS 6, sharing content on social networks like facebook, twitter wasn’t that easy. In iOS 6, however, social media integration has been made easy and can be done very quickly. In iOS 6, the social framework supports integration with Facebook, Twitter and Sina Weibo. It supports twitter is the obvious reason that twitter framework is depreciated in iOS 6. Sina Weibo is a very popular micro blogging platform in China.

For integrating social framework, there are two classes which needed to understand.
  • SLRequest
  • SLComposeViewController

These are similar with two classes TWRequest and TWComposeViewController of Twitter framework.
SL indicates that it is part of the Social framework.

iOS 6 Social Framework


SLRequest :

  • SLRequest class is a part of the Social Framework included in iOS 6.
  • SLRequest is used to construct HTTP requests.
  • It takes properties and request method of http request and provides a simple template for constructing requests.
  • Then this request is sent to APIs of Facebook, Twitter or Sina Weibo and application will receive according data in response.
  • Thus SLRequest is used to perform functions allowed by APIs of social networks.

SLComposeViewController :

  • SLComposeViewController class is used to share content with supported social networks, Facebook, Twitter and Sina Weibo.
  • It presents a view to compose a post for Facebook, Twitter or Sina Weibo.

Account framework is also used along with social framework to get account information which is set in settings of device.
Account framework provides access to user account details stored in the accounts database.

Let’s take an example of how to use SLRequest and SLComposeViewController.

There are three types of service.
  1. SLServiceTypeFacebook
  2. SLServiceTypeTwitter
  3. SLServiceTypeSinaWeibo

To post text and image, first you need to check service is available or not using is Available For Service Type method of SLComposeViewController.

If service is available (user has already logged-on required social network on device), present SLComposeViewController and post text and image.

iPhone iOS 6 Facebook and Twitter Integration


A) Post Text And Image On Facebook:

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

// initialize controller
SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

// set block for checking share/cancel button clicked in SLComposeViewController
SLComposeViewControllerCompletionHandler slBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(@"Cancelled");
} else {
NSLog(@"Post Succeed");
}
[ composeViewController dismissViewControllerAnimated:YES completion:Nil];
};
composeViewController.completionHandler = slBlock;

// set text and image, also modifiable in presented view
[composeViewController setInitialText:@”Hello”];
[composeViewController addImage:[UIImage imageNamed:@”Test.png”]];
[self presentViewController: composeViewController animated:YES completion:nil];
}


B) Post Text And Image On Twitter:

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
// initialize controller
SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

SLComposeViewControllerCompletionHandler slBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(@"Cancelled");
} else {
NSLog(@"Post Succeed");
}
[composeViewController dismissViewControllerAnimated:YES completion:Nil];
};
composeViewController.completionHandler = slBlock;

[composeViewController setInitialText:@”Hello”];
[composeViewController addImage:[UIImage imageNamed:@”Test.png”]];
[self presentViewController: composeViewController animated:YES completion:nil];

}

iPhoneQualityApplications is an extension of SPEC INDIA - an ISO 9001:2008 certified global software outsourcing and custom application development company based in Ahmedabad, India. We provide custom iPhone mobile application development to esteemed global clients with fully successful offshore application development and delivery model. Here we develop high quality iPhone applications at affordable cost. Feel free to discuss with us at lead@iphonequalityapplications.com for your business requirements.



0 comments:

Post a Comment