2

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?

Donald Duck
  • 7,638
  • 19
  • 69
  • 90
FactorJose
  • 451
  • 2
  • 6
  • 14

2 Answers2

5

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

Donald Duck
  • 7,638
  • 19
  • 69
  • 90
lfmn
  • 386
  • 3
  • 6
2

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!

Donald Duck
  • 7,638
  • 19
  • 69
  • 90
Vick Swift
  • 2,659
  • 14
  • 17