到目前为止,在我的委托中,我已经成功地从通知中获得了URL,我遇到的问题是如何将url传递给我的viewcontroller.swift,以便在webview中打开url。我已经尝试过堆栈中的几个示例,但它们似乎不能用于快速4.2。有谁可以帮我?对不起,我对斯威夫特不熟悉。
AppDelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let data = userInfo as! [String: AnyObject]
let state = UIApplication.shared.applicationState
if state == .background {
// background
print("==== Active Running ====")
if let aps = data["aps"] {
let url = aps["url"]
}
}
else if state == .inactive {
// inactive
print("==== Inactive Running ====")
if let aps = data["aps"] {
let url = aps["url"]
}
}
else if state == .active {
// foreground
print("==== Foreground Running ====")
if let aps = data["aps"] {
let url = aps["url"]
}
}
}发布于 2018-12-15 02:42:07
如果您要从头开始初始化该视图控制器,您只需在应用程序委托中这样做,在这里您可以通过以下方式获得URL:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "yourViewControllerID") as! YourViewController
vc.url = url
self.window?.rootViewController = vc同样在YourViewController文件中,将您的url设置为:
var url = "" {
didSet {
//trigger your webView to start loading, you can also do it at viewDidAppear maybe.
//example:
let url = URL(string: url)
let request = URLRequest(url: url)
webView.loadRequest(request)
}
}https://stackoverflow.com/questions/53789007
复制相似问题