Why this happens
On iPhone, UIActivityViewController appears as a full-screen modal sheet. On iPad, however, the system presents it as a popover anchored to a specific location in your interface. Since iOS 8, this popover behavior is managed by UIPopoverPresentationController, which requires an anchor point — either a sourceView with sourceRect, a barButtonItem, or both — before presentation begins.
If you present the activity controller on iPad without configuring its popoverPresentationController, the system throws an NSGenericException with the message:
UIPopoverPresentationController should have a non-nil sourceView or barButtonItem set before the presentation occurs.
This crash does not occur on iPhone because the presentation style differs. The fix is to detect iPad (or any regular horizontal size class) and provide the required anchor information.
How to fix it
1. Configure popoverPresentationController before presenting (recommended)
The modern, concise approach works on all current iOS versions. Access the popoverPresentationController property on the activity controller and set sourceView and sourceRect (or barButtonItem).
import UIKit
func presentShareSheet(from view: UIView, items: [Any]) {
let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
// Exclude unwanted activity types if needed
activityVC.excludedActivityTypes = [.airDrop, .assignToContact]
// Configure popover for iPad / Mac Catalyst
if let popover = activityVC.popoverPresentationController {
popover.sourceView = view
popover.sourceRect = view.bounds
// Optional: control arrow direction
popover.permittedArrowDirections = [.up, .down]
}
present(activityVC, animated: true)
}
Usage example — call from a button action:
@IBAction func shareButtonTapped(_ sender: UIButton) {
let text = "Check out this app!"
let url = URL(string: "https://example.com")!
presentShareSheet(from: sender, items: [text, url])
}
When the anchor is a UIBarButtonItem, use barButtonItem instead:
if let popover = activityVC.popoverPresentationController {
popover.barButtonItem = sender as? UIBarButtonItem
}
2. Legacy device-idiom check (older codebases)
Some older projects still branch on UIUserInterfaceIdiom. This works but is less flexible than the size-class approach above.
func presentShareSheetLegacy(items: [Any]) {
let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
if let popover = activityVC.popoverPresentationController {
popover.sourceView = self.view
popover.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
popover.permittedArrowDirections = []
}
}
present(activityVC, animated: true)
}
Prefer fix #1 — it adapts automatically to iPad split-view, Slide Over, and Mac Catalyst windows where the horizontal size class may be compact.
How to prevent it
- Always configure
popoverPresentationControlleron any view controller that may present as a popover (UIActivityViewController,UIAlertControllerwith.actionSheetstyle, custom popovers). - Use
sourceView+sourceRectfor view-anchored popovers; usebarButtonItemfor toolbar/navigation bar buttons. - Test on iPad in both full-screen and split-view modes; the required anchor may change with size class.
- Adopt the modern
popoverPresentationControllerAPI rather than the deprecatedUIPopoverControllerclass.
FAQ
Does this crash happen on iPhone?
No. On iPhone (compact horizontal size class), UIActivityViewController uses a full-screen modal presentation that does not involve UIPopoverPresentationController. The crash only appears on iPad or when the trait collection reports a regular horizontal size class.
Can I use the same code for Mac Catalyst?
Yes. Mac Catalyst apps run with a regular horizontal size class, so the same popoverPresentationController configuration is required. The anchor view or bar button item works identically.
What if I don’t have a natural anchor view?
Create a zero-size sourceRect centered in your main view, or anchor to the window’s root view. The popover will appear centered without an arrow when permittedArrowDirections is set to an empty array.
popover.sourceView = view.window?.rootViewController?.view ?? view
popover.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
popover.permittedArrowDirections = []
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.