The share sheet lets users easily share content from your app to other apps or services. It uses a simple popup with sharing options.
Share sheet (UIActivityViewController) in 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.
let text = "Hello from my app!"
let activityVC = UIActivityViewController(activityItems: [text], applicationActivities: nil)
present(activityVC, animated: true)if let image = UIImage(named: "photo.png") { let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil) present(activityVC, animated: true) }
let url = URL(string: "https://example.com")!
let activityVC = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(activityVC, animated: true)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.
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) } }
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.
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.
Practice
UIActivityViewController in an iOS app?Solution
Step 1: Understand the role of UIActivityViewController
UIActivityViewController is designed to present a share sheet for sharing content.Step 2: Compare with other options
Options A, B, and D relate to different app functions not handled by UIActivityViewController.Final Answer:
To present a share sheet allowing users to share content -> Option BQuick Check:
Share sheet = UIActivityViewController [OK]
- Confusing UIActivityViewController with navigation or settings controllers
- Thinking it manages background tasks
- Assuming it customizes UI elements beyond sharing
UIActivityViewController with a text string to share?Solution
Step 1: Recall the initializer signature
The correct initializer isUIActivityViewController(activityItems: [Any], applicationActivities: [UIActivity]?).Step 2: Match the correct syntax
UIActivityViewController(activityItems: ["Hello World"], applicationActivities: nil) matches the correct syntax with an array of items and nil for activities.Final Answer:
UIActivityViewController(activityItems: ["Hello World"], applicationActivities: nil) -> Option AQuick Check:
Initializer uses activityItems array [OK]
- Passing a single string instead of an array
- Using wrong parameter names like items or shareItems
- Omitting applicationActivities parameter
let textToShare = "Check this out!" let activityVC = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil) present(activityVC, animated: true)
Solution
Step 1: Analyze the code behavior
The code creates a UIActivityViewController with a text item and presents it modally.Step 2: Understand UIActivityViewController presentation
Presenting it modally shows the share sheet with sharing options for the text.Final Answer:
A share sheet appears allowing the user to share the text -> Option CQuick Check:
Presenting UIActivityViewController shows share sheet [OK]
- Assuming delegate is required to present share sheet
- Thinking share sheet appears automatically without present()
- Confusing share sheet with clipboard copy
let image = UIImage(named: "photo.png") let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil) present(activityVC, animated: true)
Solution
Step 1: Check UIImage initialization
UIImage(named:) returns an optional and may be nil if the image is missing.Step 2: Understand impact on activityItems
Passing nil in activityItems array results in no shareable content.Final Answer:
UIImage named "photo.png" may be nil -> Option DQuick Check:
UIImage(named:) can be nil if image missing [OK]
- Assuming delegate is required for UIActivityViewController
- Passing single item instead of array to activityItems
- Calling present() on wrong object
UIActivityViewController. Which code snippet correctly sets this up and presents the share sheet?Solution
Step 1: Verify correct initializer usage
UIActivityViewController requires activityItems as an array of items to share and applicationActivities as optional custom activities.Step 2: Check each option's parameters
let url = URL(string: "https://example.com")! let message = "Check this out!" let activityVC = UIActivityViewController(activityItems: [url, message], applicationActivities: nil) present(activityVC, animated: true) correctly passes an array with URL and text, and nil for applicationActivities.Step 3: Confirm safe URL unwrapping
let url = URL(string: "https://example.com")! let message = "Check this out!" let activityVC = UIActivityViewController(activityItems: [url, message], applicationActivities: nil) present(activityVC, animated: true) force unwraps URL safely assuming it exists, which is common in examples.Final Answer:
let url = URL(string: "https://example.com")! let message = "Check this out!" let activityVC = UIActivityViewController(activityItems: [url, message], applicationActivities: nil) present(activityVC, animated: true) -> Option AQuick Check:
Pass array of items to activityItems [OK]
- Passing items or applicationActivities incorrectly
- Not using an array for activityItems
- Confusing applicationActivities with share content
Ready to prove it?
Pass the test and this pattern moves unknown
