简体   繁体   English

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

[英]Trying to Open SFSafariViewController in AppDelegate via Push Notification

I am trying to open a URL using Safari when the app gets a push notification. 当应用收到推送通知时,我正在尝试使用Safari打开URL。 This works successfully when the app is in the first viewcontroller. 当应用程序在第一个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. ) (AdViewController是我的第一个视图控制器,因此仍专注于该视图控制器。)

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)
}

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: 从页面底部的“ swift 3”答案中获取在Appdeleagte中使用扩展UIApplication的最顶级UIViewController ,并将代码更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM