从AppDelegate在WkWebView中打开URL

时间:2018-12-10 12:08:35

标签: ios swift

我有一个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!))
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

输入您的控制器名称,而不是“ 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()
}