Mega Bundle SALE is ON! Get ALL of our amazing iOS app codebases at 95% OFF discount ūüĒ•

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) {
            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.

Categories: Swift programming


Bernardo · July 5, 2017 at 3:54 pm

I spent a lot of time to find something similar to

Savannah · July 5, 2017 at 8:09 pm

This is actually useful, thanks.

Louis · July 13, 2017 at 1:46 pm

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.

    florian · July 14, 2017 at 5:12 pm

    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

kel · October 7, 2017 at 1:13 am

Awesome post very useful Thanks!

malik · April 12, 2018 at 7:12 am

can we post video on facebook using above example

C√©dric Soubri√© · July 25, 2018 at 9:31 am

Are you sure you can set both and image and an url for Facebook sharing ? It seems it’s not working anymore (and initialText is definitely not working anymore)

Mike · October 26, 2018 at 2:17 pm

Even targeting 10.3, I can’t get past

[core] Sheet not being presented, calling premature completion

Leave a Reply

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

Shopping Cart