首页 文章

从AppDelegate打开WkWebView中的URL

提问于
浏览
0

我有一个网址(通过OneSignal通知),我想在我的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

    编写控制器名称而不是“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()
    }
    
  • 0

    在AppDelegate中获取一个变量,并在ViewController中使用以下参数访问它 .

    In 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!))
    }
    

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

相关问题