What are the steps I need to follow to use iOS 6’s new SLComposeViewController to post to Facebook, Twitter or Sina Weibo?
What are the steps I need to follow to use iOS 6’s new SLComposeViewController
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For details on this framework please see Apple’s Social Framework Class Reference
Additional tutorials:
For this example, we will be using the
SLComposeViewController‘sSLServiceTypeFacebook. If you wish to use Twitter or SinaWeibo just change out the SLServiceType to one of the following:iOS 6 has made it very easy to post directly to Facebook, Twitter or Sina Weibo using the
SLComposeViewController. This works very similarly to iOS 5’sTWTweetComposeViewController.First, in your view controller’s header file (.h)
#importthe Social Framework and the Accounts Framework.#import <Social/Social.h>#import <Accounts/Accounts.h>Here we will declare a simple
UIButtonand anIBActionthat we will later link to that button and avoid(sharingStatus) which will be used to check that the selected sharing service is available.Then, in your implementation file (.m), we’ll start by implementing the (sharingStatus) void that we declared in the header file. sharingStatus uses
SLComposeViewController‘sisAvailableForServiceTypeBOOL to return whether or not you can post to the service specified in its argument. In this case, we will use the service typeSLServiceTypeFacebook. If the service is available the post button will be enabled with an alpha value of 1.0f, and if the service isn’t available the button will be disabled its alpha value set to 0.5f.Here we will set up the
IBActionthat will call up the composer. For good practice, we will checkisAvailableForServiceTypeagain to avoid calling up the composer for a service type that isn’t available. (Incase something went wrong during the last check, or if availability somehow changed in the fraction of a second in between tapping the post button and the composers all/init. The code below has been set up to display a Facebook composers sheet with text, an image, and a link. This action also utilises a completion handler for the composer’s cancelled and done results.In
viewWillAppearwe will register an observer toACAccountStoreDidChangeNotificationso the application can be notified when account information changes. This observer will then be removed inviewDidDisappear.And finally, open up interface builder and add a
UIButtonwhich will be the post button. Then in the connections inspector link theIBOutletandIBActionwe created earlier to the button, and that’s it! You’re done!