3

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

Fabian
  • 13,603
  • 6
  • 31
  • 53

2 Answers2

8

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)
    }
  • 2
    Nope there isn't any other view controller presented at that moment. Your code perfectly showed my that that func was being called twice in quick succession. Once for touches began, and once for touches ended. Doh! – Fabian Jun 19 '15 at 10:44
0

I get same error message, because the UILongPressGestureRecognizer call many times my selector. Now I call UIActivityViewController when UILongPressGestureRecognizer state is end.

@objc func longTouch(gestureRecognizer: UILongPressGestureRecognizer){
    print("long touch")
    if gestureRecognizer.state != .ended {
         print("long touch not ended")
        return
    }

    //open UIActivityViewController
}
herry
  • 1,708
  • 3
  • 17
  • 30