
Apple has integrated Twitter login support since iOS 5, and Facebook since iOS 6. In the past, developers had to integrate the full Facebook and Twitter SDK to integrate sharing in their apps. Since it’s built in, it’s much easier to add these social features to your own app.
Note: If you would like to learn how to share with the native Facebook SDK, read this article: Tutorial: How To Share in Facebook SDK 4.0 for Swift
Using the Social Framework allows applications to interact with social networks from a single API without having to manage authentication. Users login to Facebook and Twitter at the OS level within the “Settings” app, so you don’t need to integrate the full Facebook or Twitter SDK and handle login yourself, it’s already done for you. It includes a system provided view controller for composing posts as well as an abstraction that allows consuming each social network’s API over HTTP.
The Social framework comes with a class named SLComposeViewController. The SLComposeViewController class presents a standard view controller for users to compose a tweet or Facebook post. It also allows developers to preset the initial text, attach images and add URL to the post. If you just want to implement simple sharing feature, this is the only class you need to know.
Below is what the SLComposeViewController looks like in your app.
For both, you’ll need to import the Social Framework.
import Social
Share on Twitter
- Go to the Storyboard and design the user interface. In the view, add the Facebook button.
- Create an action method below and name it as “twitterButtonPushed”. This method will be invoked when the button detects a Touch Up Inside event.
@IBAction func twitterButtonPushed(sender: UIButton) { if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){ var twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter) twitterSheet.setInitialText("Share on Twitter") self.presentViewController(twitterSheet, animated: true, completion: nil) } else { var alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } }
- In the Storybaord, right click the Twitter button or switch to the Connections inspector in the right pane. Next drag a connection between “Touch Up Inside” and the View Controller on the left where the “twitterButtonPushed” method is defined.
- Test it out, run your app and when you press the Facebook button you should see the SLComposeViewController popup so the user can enter their text to share on their Twitter stream. Be sure to set the initial text value to whatever you like.
Share on Facebook
- Go to the Storyboard and design the user interface. In the view, add the Facebook button.
- Create an action method below and name it as “facebookButtonPushed”. This method will be invoked when the button detects a Touch Up Inside event.
@IBAction func facebookButtonPushed(sender: UIButton) { if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){ var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook) facebookSheet.setInitialText("Share on Facebook") self.presentViewController(facebookSheet, animated: true, completion: nil) } else { var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } }
- In the Storybaord, right click the Facebook button or switch to the Connections inspector in the right pane. Next drag a connection between “Touch Up Inside” and the View Controller on the left where the “facebookButtonPushed” method is defined.
- Test it out, run your app and when you press the Facebook button you should see the SLComposeViewController popup so the user can enter their text to share on their Facebook page. Be sure to set the initial text value to whatever you like.
You can grab the full source code for this tutorial. Note: Built using Xcode 6.1.