I have come across what looks like a situation that most people face when trying to present a UIActivityViewController
on the iPad; it is crashing with:
Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7fc4f2d87d00>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.
Here's my code:
- (void)shareLeaflet
{
NSString *forwardedString = [[NSString alloc] initWithFormat:@"Check out this leaflet\n\n %@ \n\nself.theURLToShare];
UIActivityViewController *activityViewController = nil;
if (IDIOM == IPAD)
{
NSLog(@"iPad");
activityViewController.popoverPresentationController.sourceView = self.view;
// activityViewController.popoverPresentationController.sourceRect = self.frame;
[self presentViewController:activityViewController
animated:YES
completion:nil];
}
else
{
NSLog(@"iPhone");
activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
}
In my viewDidLoad
, I have:
UIBarButtonItem *composeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareLeaflet)];
self.navigationItem.rightBarButtonItem = composeButton;
}
This view is a UIPageViewController
which is showcasing some images and when the user hits the share button, I'm expecting the iOS 8 style share sheet to popup. This is exactly what happens on the iPhone, but on the iPad, it continues to crash. That led me to Stack Overflow, but none of the questions (crash on showing UIPopOverPresentationController, iOS Crash: Terminating app due to uncaught exception reason: UIPopoverPresentationController should have a non-nil sourceView, UIWebViewTerminating app due to UIPopoverPresentationController, ios8 iPad uiwebview crashes while displaying popover when user taps drop down list HTML select tag, etc) work for me.
I have tried all of the solutions in there and I just quite get what is required with this.
This is what I'm trying to achieve:
Any thoughts on this would be really appreciated.
On iPad, you must present the view controller in a popover. Therefore, on iPad, you must configure `popoverPresentationController` by providing either a sourceView and sourceRect or a barButtonItem.
A view controller that you use to offer standard services from your app.
best solve for iPad & iPhone iOS 14.4
let urlstring = "https://apps.apple.com/ae/app/"
let text = "some text for your app"
let url = NSURL(string: urlstring)
let textToShare = [url!,text] as [Any]
let activityViewController = UIActivityViewController(activityItems: textToShare as [Any], applicationActivities: nil)
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ,UIActivity.ActivityType.postToFlickr,UIActivity.ActivityType.postToTwitter,UIActivity.ActivityType.postToVimeo,UIActivity.ActivityType.mail,UIActivity.ActivityType.addToReadingList]
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = view.bounds
activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down
UIApplication.shared.windows.first?.rootViewController?.present(activityViewController, animated: true, completion: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With