1

I have a class with UIView and want to share a URL link, what is the proper way of adding the share sheet in UIView?

My current code is attempting to call the share sheet via a button click:

//share app
@IBAction func shareAppBtn(sender: AnyObject ) {
    print("shareAppBtn tapped")
    let myWebsite = NSURL(string:"http://www.google.com/")

    guard let url = myWebsite else {
        print("nothing found")
        return
    }

    let shareItems:Array = [url]
    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    self.window?.rootViewController!.presentViewController(activityViewController, animated: true, completion: nil)

}

I tried to use the root view of the current UIView but I receive an error:

Warning: Attempt to present <UIActivityViewController: on <RAMAnimatedTabBarController.RAMAnimatedTabBarController: which is already presenting <SideMenu.UISideMenuNavigationController: Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController:)

Thanks.

user3591436
  • 161
  • 2
  • 7
  • 19
  • 1
    Why are you calling `presentViewController`on the rootViewController? Try it without the `self.window?.rootViewController!.` – heikomania Aug 31 '16 at 15:30
  • I had initially tried that actually, but that gave me an error since I'm calling it from a UIView, the error is: `Use of unresolved identifier 'presentViewController'` – user3591436 Aug 31 '16 at 15:33
  • You have to call it on a viewController, yes. Your button and your UIView and somehow in a viewController? – heikomania Aug 31 '16 at 15:39
  • My button is inside a UIView, it cannot be inside a viewController for a few reasons. – user3591436 Aug 31 '16 at 15:41
  • Maybe this threads helps you: http://stackoverflow.com/questions/15622736/using-presentviewcontroller-from-uiview – heikomania Aug 31 '16 at 15:44

1 Answers1

0

You don't add a share sheet in a UIView. A share sheet is a view controller and as such it always needs another view controller to provide a context for presentation.

You have done this by using the root view of the current view, which is already presenting a navigation controller UISideMenuNavigationController.

Try presenting from that context:

sideMenuNavigationController.presentViewController(activityViewController, animated: true, completion: nil)
ff10
  • 3,046
  • 1
  • 32
  • 55
  • Thank you but unfortunately that did not work, I get an `Use of unresolved identifier 'sideMenuNavigationController'` error. I think I get the concept of not being able to call the share sheet because I'm in a UIView, this would be easier if it were another view controller. – user3591436 Aug 31 '16 at 16:02
  • Please check how that navigation controller actually is named in your code. That was just a guess coming from the context of your error message. – ff10 Aug 31 '16 at 16:03
  • Ahh my bad, trying to call the Navigation Controller that has the root view for `SideMenu` class is `UISideMenuNavigationController`, however I get the same error using that as well: `Use of unresolved identifier 'UISideMenuNavigationController'` – user3591436 Aug 31 '16 at 16:06
  • Somewhere in your view hierarchy there is a UISideMenuNavigationController that seems to be on the top of the view hierarchy. Can you try find it and present from that view controller? – ff10 Aug 31 '16 at 16:09
  • `UISideMenuNavigationController` is a Navigation controller that I created in Story Board which has a root view for `SideMenu`, how do I present this Navigation Controller inside my `SideMenu` UIView? – user3591436 Aug 31 '16 at 16:11