0
0
iOS Swiftmobile~5 mins

Share sheet (UIActivityViewController) in iOS Swift

Choose your learning style9 modes available
Introduction

The share sheet lets users easily share content from your app to other apps or services. It uses a simple popup with sharing options.

When you want users to share text, images, or files from your app.
When you want to let users send content via email, messages, or social media.
When you want to offer printing or saving options for content.
When you want to let users copy content to the clipboard or save to files.
When you want a standard, familiar way to share without building custom UI.
Syntax
iOS Swift
let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
present(activityVC, animated: true)

activityItems is an array of data to share, like strings or images.

You present the UIActivityViewController modally to show the share sheet.

Examples
Share simple text using the share sheet.
iOS Swift
let text = "Hello from my app!"
let activityVC = UIActivityViewController(activityItems: [text], applicationActivities: nil)
present(activityVC, animated: true)
Share an image loaded from your app bundle.
iOS Swift
if let image = UIImage(named: "photo.png") {
  let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
  present(activityVC, animated: true)
}
Share a web link using the share sheet.
iOS Swift
let url = URL(string: "https://example.com")!
let activityVC = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(activityVC, animated: true)
Sample App

This app shows a button labeled "Share Text" in the center. When tapped, it opens the share sheet with a text message ready to share.

iOS Swift
import UIKit

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white

    let shareButton = UIButton(type: .system)
    shareButton.setTitle("Share Text", for: .normal)
    shareButton.addTarget(self, action: #selector(shareText), for: .touchUpInside)
    shareButton.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(shareButton)

    NSLayoutConstraint.activate([
      shareButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
      shareButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
    ])
  }

  @objc func shareText() {
    let textToShare = "Hello! This is a message from my app."
    let activityVC = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil)
    activityVC.popoverPresentationController?.sourceView = self.view
    present(activityVC, animated: true)
  }
}
OutputSuccess
Important Notes

On iPad, set popoverPresentationController?.sourceView to avoid crashes.

You can share multiple items by adding them to the activityItems array.

The share sheet automatically shows only the apps that can handle the data type you provide.

Summary

The share sheet is a simple way to let users share content from your app.

Use UIActivityViewController with the items you want to share.

Present it modally and it shows a familiar popup with sharing options.