IOS 8 iPad App Crashes When UIActivityViewController Is Called

前端 未结 5 438
借酒劲吻你
借酒劲吻你 2020-12-19 04:08

When a UIActivityViewController is called on the iPhone in this app, it works perfectly, but when called on a iPad, the app crashes. Below is the code I used:



        
相关标签:
5条回答
  • 2020-12-19 04:31

    Before presenting the UIActivityViewController, add in this line of code:

    activityViewController.popoverPresentationController?.sourceView = self.view
    

    This way, the view controller knows in which frame of the GameViewController to appear in.

    0 讨论(0)
  • 2020-12-19 04:42

    There are two option, the action came from a UIBarButtonitem or UIButton that is a UIView.

    func shareButtonPress() {
    
        ...
    
        if let actv = activityViewController.popoverPresentationController {
            actv.barButtonItem = someBarButton // if it is a UIBarButtonItem
    
            // Or if it is a view you can get the view rect
            actv.sourceView = someView
            // actv.sourceRect = someView.frame // you can also specify the CGRect
        }
    
        self.presentViewController(activityViewController, animated: true, completion: nil)
    }
    

    You may have to add a sender to your function like func shareButtonPress(sender: UIBarButtonItem) or func shareButtonPress(sender: UIButton)

    0 讨论(0)
  • 2020-12-19 04:44

    If you read the error it says how to fix it, you need to set the barButtonItem or sourceView from which to present the popover from, in your case:

    func shareButtonPress(pressedButton: UIBarButtonItem) { 
    
        ...
    
        activityViewController.popoverPresentationController.barButtonItem = pressedButton
    
        self.presentViewController(activityViewController, animated: true, completion: nil)
    }
    
    0 讨论(0)
  • 2020-12-19 04:47

    Swift 5:

    Check if the device is iPhone or iPad and based on that add a sourceView and present the activityController

    let activity = UIActivityViewController(activityItems: [self], applicationActivities: nil)
    if UIDevice.current.userInterfaceIdiom == .phone {
        UIApplication.topViewController?.present(activity, animated: true, completion: nil)
    } else {
        activity.popoverPresentationController?.sourceView = UIApplication.topViewController!.view
        UIApplication.topViewController?.present(activity, animated: true, completion: nil)
    }
    
    0 讨论(0)
  • 2020-12-19 04:55

    I added for Swift 3:

    activityViewController.popoverPresentationController?.sourceView = self.view
    

    fixed my issue.

    0 讨论(0)
提交回复
热议问题