ios - 如何从 swift 4.2 中的通知传递 url 以在 webview 中打开

标签 ios swift webview

到目前为止,在我的委托(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/

相关文章:

ios - 使用 SnapKit 的自动布局未按预期响应

ios - iPhone 的 Urban Airship 推送通知 : SSL Problem

android - 单击 WebView 上的事件坐标

swift - (os/kern) invalid capability (20) invalid name(15) - 我似乎无法摆脱这些错误

arrays - 从单例创建一个通用数组

ios - Xamarin iOS webview Zoom 不起作用

android - SslErrorHandler proceed() 函数的实现

ios - 禁用复制,在 UITextfield 中粘贴在 iOS 9.x 中不起作用

ios - Facebook 应用邀请对话框无法正常工作

swift - 为什么 UIActivityViewController 在控制台中显示自动约束错误