Warning when presenting UIActivityViewController

2019-07-24 02:13发布

问题:

When i present an UIActivityController using the code below i get, it is presented but the console shows "Warning: Attempt to present <UIActivityViewController: 0x7f8788e7aed0> on <MyApp.CustomTableViewController: 0x7f8788e3db60> which is already presenting (null)".

@IBAction func shareImage(sender: AnyObject) {
    let images: [UIImage] = [image.image!]
    let activityViewController = UIActivityViewController(activityItems: images, applicationActivities: nil)
    self.presentViewController(activityViewController, animated: true, completion: nil)
}

This func is called by an UILongPressGestureRecognizer. Note that i'm using storyboard with the following hierarchy:

TabBarController > (Relationship) > NavigationController > (Relationship) > TableViewController > (Show) > TableViewController > (Show) > ViewController.

The presentation happens on the last ViewController.

I'm quite sure it's about the hierarchy, which controller is currently presenting (and maybe how) and which controller is responsible for presenting the UIActivityViewController.

EDIT

UILongPressGestureRecognizer touch event is called multiple times which was causing the warning

回答1:

It's hard to say from your question but is there some other view controller presented at the moment this happens? for example and action sheet or other?

In any case try this:

    if self.presentedViewController != nil {
        self.dismissViewControllerAnimated(false, completion: {
            [unowned self] in
            self.presentViewController(activityViewController, animated: true, completion: nil)
            })
    }else{
        self.presentViewController(activityViewController, animated: true, completion: nil)
    }