Reputation: 491
I'm trying to add a button in order to share some sentences in Twitter, Facebook... etc. It all works on all iPhone models but simulator crash with an iPad.
This is my code:
@IBAction func shareButton(sender: AnyObject) {
frase = labelFrases.text!
autor = labelAutores.text!
var myShare = "\(frase) - \(autor)"
let activityVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
And this is the error:
Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7c0f9190>) should have a non-nil sourceView or barButtonItem set before the presentation occurs
How should I solve it?
Upvotes: 5
Views: 3070
Reputation: 61
Slightly adapted version to make it work on any button, iPad and iPhone.
Xcode 13.4.1 (Swift 5.6)
let itemToShare = ["Some Text goes here"]
let avc = UIActivityViewController(activityItems: itemToShare, applicationActivities: nil)
//Apps to be excluded sharing to
avc.excludedActivityTypes = [
UIActivity.ActivityType.print,
UIActivity.ActivityType.addToReadingList
]
// Check if user is on iPad and present popover
if UIDevice.current.userInterfaceIdiom == .pad {
if avc.responds(to: #selector(getter: UIViewController.popoverPresentationController)) {
avc.popoverPresentationController?.sourceView = sender as? UIView
}
}
// Present share activityView on regular iPhone
self.present(avc, animated: true, completion: nil)
Upvotes: 2
Reputation: 3035
Do this instead for Swift 5 to get share button working on both iPad and iPhone:
@IBAction func shareButton(sender: UIButton) { {
let itemToShare = ["Some Text goes here"]
let avc = UIActivityViewController(activityItems: itemToShare, applicationActivities: nil)
//Apps to be excluded sharing to
avc.excludedActivityTypes = [
UIActivityType.print,
UIActivityType.addToReadingList
]
// Check if user is on iPad and present popover
if UIDevice.current.userInterfaceIdiom == .pad {
if avc.responds(to: #selector(getter: UIViewController.popoverPresentationController)) {
avc.popoverPresentationController?.barButtonItem = sender
}
}
// Present share activityView on regular iPhone
self.present(avc, animated: true, completion: nil)
}
Hope this helps!
Upvotes: 3
Reputation: 406
For ipad (iOS > 8.0) you need to set popoverPresentationController:
//check ipad
if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
//ios > 8.0
if ( activityVC.respondsToSelector(Selector("popoverPresentationController"))){
activityVC.popoverPresentationController?.sourceView = super.view
}
}
self.presentViewController(activityVC, animated: true, completion: nil)
More information here: UIActivityViewController crashing on iOS 8 iPads
Upvotes: 7