SLComposeViewController Sharing Tutorial

What steps should I follow to use iOS 6 new SLComposeViewController to post to Facebook, Twitter or Sina Weibo?

+76
ios ios6 social-framework slcomposeviewcontroller
Sep 19 '12 at 21:37
source share
3 answers

For more information about this platform, please refer to the Apple Social Framework reference class.

Extra lessons:

  1. http://soulwithmobiletechnology.blogspot.com/2012/07/tutorial-how-to-use-inbuilt.html
  2. http://www.mobile.safilsunny.com/iphone/integrating-facebook-ios/
  3. https://rudeboy-quickies.blogspot.com/2012/06/steps-to-integrate-facebook-in-ios6.html

In this example, we will use the SLComposeViewController SLServiceTypeFacebook . If you want to use Twitter or SinaWeibo, just change the SLServiceType to one of the following:

  • SLServiceTypeFacebook
  • SLServiceTypeSinaWeibo
  • SLServiceTypeTwitter

iOS 6 made it very easy to post directly to Facebook, Twitter, or Sina Weibo using the SLComposeViewController . This works very similar to iOS 5 TWTweetComposeViewController .

Firstly, in your view controller header (.h) #import Social Framework and Framework Accounts.

#import <Social/Social.h>

#import <Accounts/Accounts.h>

Here we will declare a simple UIButton and IBAction which we later UIButton with this button, and void (sharedStatus), which will be used to check the availability of the selected exchange service.

 @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIButton *easyFacebookButton; - (IBAction)facebookPost:(id)sender; - (void)sharingStatus; @end @implementation ViewController 

Then, in your implementation file (.m), we start with the implementation of vid (sharedStatus), which we declared in the header file. sharedStatus uses the SLComposeViewController isAvailableForServiceType BOOL to return whether you can post messages to the service specified in its argument. In this case, we will use a service like SLServiceTypeFacebook . If the service is available, the "post" button will be enabled with an alpha value of 1.0f, and if the service is not available, the button will be disabled, its alpha value will be set to 0.5f.

 - (void)sharingStatus { if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { NSLog(@"service available"); self.easyFacebookButton.enabled = YES; self.easyFacebookButton.alpha = 1.0f; } else { self.easyFacebookButton.enabled = NO; self.easyFacebookButton.alpha = 0.5f; } } 

Here we are an IBAction that will call the composer. For good practice, we isAvailableForServiceType check isAvailableForServiceType for isAvailableForServiceType to avoid calling isAvailableForServiceType for a service type that is not available. (If something went wrong during the last check, or if accessibility somehow changed in a split second between clicking the โ€œPostโ€ button and โ€œAll composers / init.โ€ The code below was configured to display a Facebook composers sheet with text , image, and link: This action also uses a completion handler for canceled and executed results by the composer.

 - (IBAction)facebookPost:(id)sender { if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; [mySLComposerSheet setInitialText:@"iOS 6 Social Framework test!"]; [mySLComposerSheet addImage:[UIImage imageNamed:@"myImage.png"]]; [mySLComposerSheet addURL:[NSURL URLWithString:@"http://stackoverflow.com/questions/12503287/tutorial-for-slcomposeviewcontroller-sharing"]]; [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) { switch (result) { case SLComposeViewControllerResultCancelled: NSLog(@"Post Canceled"); break; case SLComposeViewControllerResultDone: NSLog(@"Post Sucessful"); break; default: break; } }]; [self presentViewController:mySLComposerSheet animated:YES completion:nil]; } } 

In viewWillAppear we register an observer in ACAccountStoreDidChangeNotification so that the application can receive notifications about changes to account information. This observer will be deleted in viewDidDisappear .

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sharingStatus) name:ACAccountStoreDidChangeNotification object:nil]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:ACAccountStoreDidChangeNotification]; } 

And finally, open the interface constructor and add a UIButton which will be the post button. Then in the connection inspector, connect the IBOutlet and IBAction created earlier with the button, and here it is! You made!

enter image description here

+141
Sep 19 '12 at 21:37
source share

Just use this code to post to Facebook.

 SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; [controllerSLC setInitialText:@"First post from my iPhone app"]; [controllerSLC addURL:[NSURL URLWithString:@"http://www.appcoda.com"]]; [controllerSLC addImage:[UIImage imageNamed:@"test.jpg"]]; [self presentViewController:controllerSLC animated:YES completion:Nil]; 

If you want this for Twitter, just change the SLServiceTypeTwitter.

+25
Feb 15 '13 at 9:06
source share

Safe use of SLComposeViewController

 if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { SLComposeViewController *fbPost = [SLComposeViewController composeViewControllerForServiceType: SLServiceTypeFacebook]; [fbPost setInitialText:@"Text You want to Share"]; [fbPost addImage:[UIImage imageNamed:@"shareImage.png"]]; [self presentViewController:fbPost animated:YES completion:nil]; [fbPost setCompletionHandler:^(SLComposeViewControllerResult result) { switch (result) { case SLComposeViewControllerResultCancelled: NSLog(@"Post Canceled"); break; case SLComposeViewControllerResultDone: NSLog(@"Post Sucessful"); break; default: break; } [self dismissViewControllerAnimated:YES completion:nil]; }]; } 
+13
Jun 20 '13 at 6:28
source share



All Articles