3

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
  • 8,409
  • 22
  • 75
  • 99
FactorJose
  • 471
  • 2
  • 6
  • 14

3 Answers3

6

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
  • 8,409
  • 22
  • 75
  • 99
lfmn
  • 396
  • 3
  • 6
3

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
  • 8,409
  • 22
  • 75
  • 99
Vick Swift
  • 2,945
  • 1
  • 15
  • 17
1

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)
Pix
  • 51
  • 3