尝试通过推送通知在AppDelegate中打开SFSafariViewController

时间:2016-12-09 16:21:44

标签: ios objective-c swift3

我正在尝试使用Safari在应用获取推送通知时打开网址。当应用程序位于第一个viewcontroller中时,这可以成功运行。但如果应用程序进一步进入程序,我会收到错误

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

(AdViewController是我的第一个视图控制器,所以它仍然专注于那个。)

我搜索了这个网站,并尝试了所有关于此错误的建议,但是已经很短了。目前,我的代码如下所示:

if MessageUrl != nil , let url = URL(string: MessageUrl!) {
   let safari = SFSafariViewController(url: url)
                self.window?.rootViewController?.presentedViewController?.present(safari, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

Get top most UIViewController我在&#34; swift 3&#34;中使用了Appdeleagte中的扩展UIApplication。在该页面底部回答并将我的代码更改为:

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