soia019192
soia019192

Reputation: 43

Open url in WkWebView from AppDelegate

I have an url (through OneSignal notifications) and I want to load the url in my ViewController.webView (WkWebView). How can I achieve this? Must I make the webView a static variable?

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

Upvotes: 1

Views: 1833

Answers (2)

AtulParmar
AtulParmar

Reputation: 4570

write your controller name instead of "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)
        }
    }

OR try this one.

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()
}

Upvotes: 1

Sateesh Yemireddi
Sateesh Yemireddi

Reputation: 4399

Take one variable in AppDelegate and access it in the ViewController with following.

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

Upvotes: 0

Related Questions