到目前为止,在我的委托(delegate)中,我已经能够从通知中成功获取 URL,我遇到的问题是如何将 url 传递给我的 viewcontroller.swift 以在 webview 中打开 url。我已经尝试了堆栈中的几个示例,但它们似乎不适用于 swift 4.2。谁能帮我?对不起,我是 swift 的新手。
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"]
}
}
}
最佳答案
如果你打算从头开始初始化那个 View Controller ,你可以在你的 Appdelegate 中这样做,你可以通过以下方式获得你的 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)
}
}
关于ios - 如何从 swift 4.2 中的通知传递 url 以在 webview 中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53789007/