Skip to main content

Share content to Facebook and Twitter in Swift 5 – SLComposeViewController

· 3 min read
Full Stack Developer
Last updated on June 19, 2019

intro picture

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.

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)
}
}
}

Mega Bundle Sale is ON! Get ALL of our iOS App codebases at 90% OFF discount 🔥

Get the Mega Bundle

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.

Looking for a custom mobile application?

Our team of expert mobile developers can help you build a custom mobile app that meets your specific needs.

Get in Touch

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