SLComposeViewController
iOS Development
Social Media Integration
Code Tutorial
Swift Programming

Tutorial for SLComposeViewController sharing

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

SLComposeViewController is an older Apple API for presenting a compose sheet for certain built-in social services. If you are maintaining legacy code, it is still useful to understand, but for broad modern sharing workflows Apple generally steers developers toward the system share sheet with UIActivityViewController.

What SLComposeViewController Does

SLComposeViewController lives in the Social framework and presents a system-provided compose UI for supported services. It can prefill:

  • text
  • a URL
  • an image

The basic flow is:

  1. check whether the service is available
  2. create the compose controller
  3. add content
  4. present it

Basic Example

swift
1import UIKit
2import Social
3
4final class ShareViewController: UIViewController {
5    @IBAction func shareToTwitter() {
6        guard SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter) else {
7            print("Twitter service is not available")
8            return
9        }
10
11        let composer = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
12        composer?.setInitialText("Hello from my app")
13        composer?.add(URL(string: "https://example.com"))
14        composer?.completionHandler = { result in
15            if result == .done {
16                print("Post completed")
17            } else {
18                print("Post cancelled")
19            }
20        }
21
22        if let composer {
23            present(composer, animated: true)
24        }
25    }
26}

This is the core legacy pattern. The controller handles the compose UI, and your completion handler tells you whether the user finished or canceled.

Adding an Image

You can also attach an image before presenting the sheet.

swift
1if let composer = SLComposeViewController(forServiceType: SLServiceTypeTwitter) {
2    composer.setInitialText("Sharing an image")
3    composer.add(UIImage(named: "banner"))
4    present(composer, animated: true)
5}

As with any media-sharing UI, make sure the asset exists and is reasonably sized before you attach it.

Service Availability Matters

The availability check is not optional. A service may be unavailable because:

  • the account is not configured the way your legacy flow expects
  • the service is not supported in the current environment
  • the platform behavior has changed since the original code was written

That is why isAvailable(forServiceType:) should be the gate before creating the controller.

When to Prefer UIActivityViewController

For most new code, UIActivityViewController is the better choice because it presents the system share sheet and works across a broader set of sharing destinations and actions.

swift
1import UIKit
2
3let items: [Any] = [
4    "Hello from my app",
5    URL(string: "https://example.com")!
6]
7
8let controller = UIActivityViewController(activityItems: items, applicationActivities: nil)
9present(controller, animated: true)

This is the more future-friendly sharing model on iOS. Instead of targeting one specific social service API, you hand the system shareable items and let it present the available destinations.

Choosing Between the Two

Use SLComposeViewController only when:

  • you are maintaining legacy code
  • you specifically rely on that old compose API
  • the service type you need is still appropriate for your target environment

Use UIActivityViewController when:

  • you want general sharing
  • you want Apple's modern share-sheet flow
  • you do not want to hardcode one legacy social destination

That distinction matters because many old tutorials assume platform behavior that no longer reflects typical iOS sharing design.

UI Considerations

Do not bury sharing behind an obscure gesture or nonstandard affordance. Apple recommends using the familiar share-sheet model when possible. If you do use SLComposeViewController, present it from a clear share action and keep the prefilled content helpful but minimal.

For iPad, remember that share-related presentation may need popover configuration when you use activity controllers. The general design principle is the same: sharing should feel system-native, not custom and brittle.

Common Pitfalls

Skipping isAvailable(forServiceType:) can leave you presenting a flow that is unsupported or unusable in the current environment.

Treating SLComposeViewController as the default answer for all modern iOS sharing is outdated. The share sheet is usually the better choice now.

Prefilling too much text, attaching missing images, or force-unwrapping invalid URLs can make the sharing flow fail before the controller is even presented.

Ignoring the completion handler means you lose the chance to update UI state after the user cancels or finishes.

Summary

  • 'SLComposeViewController is a legacy Apple compose controller for certain social services.'
  • The basic flow is availability check, create controller, add content, and present it.
  • It can prefill text, URLs, and images and report completion or cancellation.
  • For most modern sharing features, prefer UIActivityViewController.
  • Use the older API mainly when you are supporting existing code that already depends on it.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.