Share content to Facebook and Twitter in Swift 3 – SLComposeViewController

We’re currently working on an iOS template that lets you create an iPhone app for your WordPress blog in a matter of minutes. While implementing this, we had to come up with a mechanism that allows users to share content to Facebook and Twitter. This is an important functionality for a blogger since everyone wants their content to be easily spread around the Internet. That’s when we discovered SLComposeViewController.

ios share content facebook twitter swift 3

SLComposeViewController is part of the Social framework, provided by Apple. It presents to the users a view that enables them to edit a post that’s going to be shared on social networks, such as Facebook and Twitter. In order to share content, you’ll provide the SLComposeViewController with information such as image, URL and initial text.

Because we wanted to create a more generic mechanism, that will be easily reused across all our templates requiring social sharing, we created a generic SocialMediaSharingManager class, so that you can trigger the social sharing flow with just one line of code. We thought this will be useful for many other iOS developers coding social apps in Swift 3, so we’re sharing our code in this blog post:

import Social
import UIKit

public protocol SocialMediaShareable {
    func image() -> UIImage?
    func url() -> URL?
    func text() -> String?
}

public class SocialMediaSharingManager {
    public static func shareOnFacebook(object: SocialMediaShareable, from presentingVC: UIViewController) {
        share(object: object, for: SLServiceTypeFacebook, from: presentingVC)
    }

    public static func shareOnTwitter(object: SocialMediaSharable, from presentingVC: UIViewController) {
        share(object: object, for: SLServiceTypeTwitter, from: presentingVC)
    }

    private static func share(object: SocialMediaShareable, for serviceType: String, from presentingVC: UIViewController) {
        if let composeVC = SLComposeViewController(forServiceType:serviceType) {
            composeVC.add(object.image())
            composeVC.add(object.url())
            composeVC.setInitialText(object.text())
            presentingVC.present(composeVC, animated: true, completion: nil)
        }
    }
}

Since we are big fans of protocol-oriented programming, we created the SocialMediaShareable protocol, that represents model objects from your application, which can be shared on Twitter or Facebook. In our specific case, these objects are news and blog posts. If you have an object that is shareable, just make it conform to this protocol and implement the three methods.

Once you have your shareable object, you can fire up Apple’s sharing view controller, with just one line of code:

SocialMediaSharingManager.shareOnFacebook(object: sharablePost, from: viewController)

Easy, right?

By using the protocol, we designed our code in a robust way, that is very easy to scale. The generic sharing manager is basically able to share all kinds of shareable objects.

Keep in mind that SLComposeViewController has support for more types of functionalities. For instance, you can handle the results of the user’s interaction via completion handler. For more details, you can check out Apple’s documentation.

As usually, please let us know if you have any suggestions or if you need any help adapting this to your own iOS project.

5 thoughts on “Share content to Facebook and Twitter in Swift 3 – SLComposeViewController

  1. the post is really interesting !

    I’m a beginner and I have a problem with the last action :
    – How to make my shareable object ?

    – I don’t understand where have i put :
    ” SocialMediaSharingManager.shareOnFacebook(object: sharablePost, from: viewController)”

    thanks for your help and sorry for my inexperience.

    • Hi Louis,

      Here’s an example of a sharable object

      struct SharablePost: SocialMediaSharable {
      private let imageObj: UIImage?
      private let urlObj: URL?
      private let textObj: String?
      init(image: UIImage?, url: URL?, text: String?) {
      self.imageObj = image
      self.urlObj = url
      self.textObj = text
      }

      // MARK: – SocialMediaSharable
      func image() -> UIImage? {
      return self.imageObj
      }

      func url() -> URL? {
      return self.urlObj
      }

      func text() -> String? {
      return self.textObj
      }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *