Share iOS content with UIActivityViewController
Adding a share sheet to an iOS app gives users a familiar way to send selected content to Messages, Mail, AirDrop, social apps, and other installed services. UIKit provides the standard controller for this job, so a small Swift implementation can cover sharing text, links, images, and files without building a separate publishing system.
UIActivityViewController is especially useful for UIKit projects and developers learning how presentation, item providers, and user choice fit together. Your app supplies the content, the operating system presents available destinations, and the user decides where that content goes. The following implementation works with ordinary UIKit view controllers and can be tested in Xcode on an iPhone and iPad.
See how the share sheet works
The share sheet is not a single destination. It is a system interface that adapts to installed apps and available services. When you pass a URL, for example, the user may choose a messaging conversation, a social network, Safari, or another compatible handler. The shared content should therefore be useful even if the selected service renders it differently.
Each item enters the controller through activityItems. A small, homogeneous array is usually easiest to maintain, although UIKit can accept common combinations such as a caption and URL. Images should be UIImage values, web links should be URL values, and plain messages should be String values. Using the correct type gives activity extensions better information about what they can publish.
For a junior developer, this API offers a useful lesson in Apple’s platform design: powerful features do not require every app to reproduce them. Apple’s documentation, sample projects, and WWDC sessions explain the controller’s behaviour, while a WWDC Swift Student Challenge project can demonstrate a focused version of the same sharing flow.
Prepare the content to share
Start by deciding what the share button represents. A detail screen might share an article title and URL; a photo screen might share an image; a task screen might share a short description. Avoid putting confidential fields into the payload merely because the array accepts strings.
A helper method keeps the content preparation clear:
func makeShareItems() -> [Any] {
let title = "Laneway coffee walk in Melbourne"
guard let url = URL(string: "https://example.com/event") else {
return [title]
}
return [title, url]
}
The URL is optional. For a message-only action, ["Meeting at 6:30 pm near Flinders Street"] is enough. For a file, use a file URL that the receiving service can read, or provide an NSItemProvider when the file needs a custom preview. A UIActivityItemSource can calculate content at presentation time, avoiding unnecessary work when the user opens and cancels the sheet.
Shared data often comes from the app’s model. If the app stores tasks with Core Data, this Core Data to-do tutorial shows how to structure and save that data before building a share payload. Keep presentation code separate from persistence code, and give the controller a plain value it can hand to UIKit.
Present the controller from a button
In Xcode’s storyboard, connect a button’s Touch Up Inside event to an action method and give it a useful accessibility label. Create the controller with the selected items, then present it from the current view controller. The applicationActivities parameter can remain nil so UIKit supplies the standard system activities.
@IBAction func shareTapped(_ sender: UIButton) {
let controller = UIActivityViewController(
activityItems: makeShareItems(),
applicationActivities: nil
)
present(controller, animated: true)
}
Calling present starts the flow, but production code should also decide what happened. completionWithItemsHandler reports the selected activity type, whether the activity completed, any returned items, and an error. Treat cancellation as a normal user choice rather than an app failure. Avoid logging shared content by default because a link or photo may contain personal information.
An activity extension can return additional data, such as a newly created contact or an edited document. The returned items are useful when the app must react to the result. Most share buttons do not need to change local state, but a completion handler remains the right place to record a user-confirmed outcome or report a genuine failure.
Support iPad and everyday layouts
On iPhone, the share sheet appears as a modal sheet. On iPad, UIActivityViewController is presented in a popover, and iPadOS requires it to have an anchor. A share button is a natural source: point the popover at the control and let the arrow indicate where the action began.
if let popover = controller.popoverPresentationController {
popover.sourceView = sender
popover.sourceRect = sender.bounds
}
Place this configuration before present(controller, animated: true). If the same action is triggered from a navigation bar item, use that bar button as the source instead. Testing both layouts matters for Australian users who may use an iPad for reading, planning, and reviewing travel details while relying on an iPhone for everyday communication.
Make sharing relevant to Australia
Australia’s geography makes sharing practical in several common situations. A person can send a meeting location from Melbourne to a colleague arriving in Sydney, share a route involving Melbourne’s train or tram network, or pass an itinerary to family travelling between Perth, Adelaide, Brisbane, and the eastern states. Distances in kilometres and dates in day–month–year order can make those messages more useful when appropriate.
Everyday mobile habits also shape the feature. Sharing a restaurant link through Messages or WhatsApp, posting a travel image to Instagram, and sending an event to a Facebook group are familiar patterns on Australian phones. Let the user choose from installed services instead of hard-coding one social network, because platform use varies by age, device, and community.
The local app market contains many travel, food-delivery, banking, and social tools, making reliable links and rich previews useful in everyday workflows. Privacy deserves the same care. The Privacy Act 1988 and the Australian Privacy Principles apply to covered organisations that collect, use, or disclose personal information. A standard share sheet does not automatically publish data on the user’s behalf, but your app may still handle personal information if it stores shared content, records analytics, or uploads a copy. Collect only what the feature needs and explain any automatic upload before it happens.
Choose the right sharing approach
UIKit remains the direct choice for an existing UIKit app. SwiftUI projects can use ShareLink on supported operating system versions, while a custom UIActivity gives complete control over a specialised publishing service. These approaches overlap, but they differ in how much platform behaviour you inherit and how much work you take on.
| Approach | Best fit | Setup | Main limitation |
|---|---|---|---|
UIActivityViewController |
UIKit apps and custom item arrays | Create, configure, and present | Requires explicit popover handling on iPad |
ShareLink |
SwiftUI apps | Add a link with a shareable value | Less control over destinations and results |
Custom UIActivity |
A branded or specialised service | Implement a custom activity class | More code, testing, and maintenance |
Use UIActivityViewController when the goal is to give users the standard Apple sharing experience. It is compact, familiar, and automatically reflects activities installed on the device. Reach for ShareLink when the interface is SwiftUI and the platform’s default behaviour meets the requirement. A custom activity is justified when the destination needs unique branding, unsupported data, or a workflow the system sheet cannot provide.
A dependable implementation starts with one clearly typed payload, presents the controller from a visible control, handles the iPad popover, and checks the completion callback without treating cancellation as an error. For a junior iOS developer, that platform-first discipline is the practical foundation to carry into larger apps.