繁体   English   中英

从AppDelegate在WkWebView中打开URL

[英]Open url in WkWebView from AppDelegate

我有一个URL(通过OneSignal通知),我想将该URL加载到ViewController.webView(WkWebView)中。 我该如何实现? 我必须将webView设为静态变量吗?

// AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]

    let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
        // This block gets called when the user reacts to a notification received
        let payload: OSNotificationPayload = result!.notification.payload

        if payload.additionalData != nil {
            let additionalData = payload.additionalData
            if additionalData?["url"] != nil {
                let url = additionalData?["url"]
                print("url: \(url)")
                // How to Open this URL?
            }
        }
    }
}

// ViewController
var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    view.frame = view.bounds

    let webConfiguration = WKWebViewConfiguration()

    webView.navigationDelegate = self
    webView.uiDelegate = self

    view.addSubview(webView)

    let myURL = URL(string: "")
    webView.load(URLRequest(url: myURL!))
}

在AppDelegate中获取一个变量,然后在ViewController中访问以下变量。

在ViewController中:

var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    view.frame = view.bounds

    let webConfiguration = WKWebViewConfiguration()

    webView.navigationDelegate = self
    webView.uiDelegate = self

    view.addSubview(webView)

    //Get the url from AppDelegate here
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let url = appDelegate.myUrl

    let myURL = URL(string: url)
    webView.load(URLRequest(url: myURL!))
}

在AppDelegate中:

var myUrl: String = "" //Create iVar here

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]

    let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
        // This block gets called when the user reacts to a notification received
        let payload: OSNotificationPayload = result!.notification.payload

        if payload.additionalData != nil {
            let additionalData = payload.additionalData
            if additionalData?["url"] != nil {
                let url = additionalData?["url"]
                print("url: \(url)")
                // Here save the url
                myUrl = url
            }
        }
    }
}

输入您的控制器名称,而不是“ YourControllerVC”

var appdelgateObj = UIApplication.shared.delegate as! AppDelegate
    if let destinationVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: “YourControllerVC”) as? YourControllerVC {
        if let window = appdelgateObj.window , let rootViewController = window.rootViewController {
            var currentController = rootViewController
            while let presentedController = currentController.presentedViewController {
                currentController = presentedController
            }
            destinationVC.webURL = "yourWebViewURLFromNotification"
            currentController.present(destinationVC, animated: true, completion: nil)
        }
    }

或尝试这个。

func redirectToWebView() {
    let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController: YourControllerVC = mainStoryboard.instantiateViewController(withIdentifier: "YourControllerVC") as YourControllerVC
    initialViewController.webURL = "yourWebViewURLFromNotification"
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
}

暂无
暂无

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

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