0

I am trying to open a URL using Safari when the app gets a push notification. This works successfully when the app is in the first viewcontroller. But if the app is further into the program I get an error

<SFSafariViewController: 0x10b20ae60> on <AdViewController: 0x100b14530> whose view is not in the window hierarchy!

(AdViewController is my first view controller, so it is still focused on that one. )

I have searched this site and have tried all the suggestions for this error but have come up short. Currently, my code looks like this:

if MessageUrl != nil , let url = URL(string: MessageUrl!) {
   let safari = SFSafariViewController(url: url)
                self.window?.rootViewController?.presentedViewController?.present(safari, animated: true, completion: nil)
}
Snorlax
  • 31
  • 1
  • 4
  • You can't present a ViewController on a presented ViewController – Eeshwar Dec 09 '16 at 16:25
  • well i had tried self.window?.rootViewController?.present same issue i guess. I know it is broken, i am just too new to understand the fix. – Snorlax Dec 09 '16 at 16:31

1 Answers1

0

Get top most UIViewController I used extension UIApplication in Appdeleagte from the "swift 3" answer at bottom of that page and changed my code to:

if MessageUrl != nil , let url = URL(string: MessageUrl!) {
            let safari = SFSafariViewController(url: url)
             UIApplication.topViewController()?.present(safari, animated: true, completion: nil)
        }
Community
  • 1
  • 1
Snorlax
  • 31
  • 1
  • 4