0
0
iOS Swiftmobile~20 mins

Share sheet (UIActivityViewController) in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Share Sheet Demo
This screen shows a button that opens the iOS share sheet to share a text message.
Target UI
-------------------------
| Share Sheet Demo       |
|-----------------------|
|                       |
|      [Share Text]      |
|                       |
-------------------------
Add a UIButton labeled 'Share Text' centered on the screen.
When the button is tapped, open a UIActivityViewController to share the text 'Hello from my app!'.
Ensure the share sheet works on both iPhone and iPad (use popoverPresentationController for iPad).
Starter Code
iOS Swift
import UIKit

class ShareSheetViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground

        // TODO: Add Share Text button and action
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
iOS Swift
import UIKit

class ShareSheetViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground

        let shareButton = UIButton(type: .system)
        shareButton.setTitle("Share Text", for: .normal)
        shareButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        shareButton.translatesAutoresizingMaskIntoConstraints = false
        shareButton.addTarget(self, action: #selector(shareText), for: .touchUpInside)

        view.addSubview(shareButton)

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

    @objc func shareText() {
        let textToShare = "Hello from my app!"
        let activityVC = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil)

        // For iPad: set popover source
        if let popover = activityVC.popoverPresentationController {
            popover.sourceView = self.view
            popover.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
            popover.permittedArrowDirections = []
        }

        present(activityVC, animated: true)
    }
}

We create a UIButton labeled 'Share Text' and center it using Auto Layout. When tapped, it triggers shareText() which creates a UIActivityViewController with the text to share. For iPad compatibility, we set the popoverPresentationController sourceView and sourceRect to avoid crashes and show the share sheet properly as a popover.

This approach uses standard UIKit patterns and ensures the share sheet works on all iOS devices.

Final Result
Completed Screen
-------------------------
| Share Sheet Demo       |
|-----------------------|
|                       |
|      [Share Text]      |
|                       |
-------------------------
User taps the 'Share Text' button.
The iOS share sheet appears with options to share the text 'Hello from my app!' via Messages, Mail, AirDrop, etc.
User can select an option or cancel to return to the screen.
Stretch Goal
Add an image to share along with the text in the share sheet.
💡 Hint
Create a UIImage from your assets and include it in the activityItems array passed to UIActivityViewController.