0

I have a tabbed app which loads separate internal file WKwebviews. I now need to have the WKwebview refresh when a tab is selected.

I think I need to add the required code in the viewWillAppear, but on trying to have some code on this method nothing works.

Does anyone have any suggestions as to how I can achieve this

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        webView.configuration.userContentController.add(self, name: "jsHandler")
        let bundleURL = Bundle.main.resourceURL!.absoluteURL
        let html = bundleURL.appendingPathComponent("main/index.html") //Loads internal HTML files
        webView.loadFileURL(html, allowingReadAccessTo:bundleURL)
        webView!.uiDelegate = self
        webView.allowsBackForwardNavigationGestures = true //Allows 'safari' style gestures swipe back etc
    }

    override func viewWillAppear(_ animated: Bool) {
        //Nothing
    }

Update: To resolve this, I added the above code into the viewDidAppear method rather than the viewDidLoad. This has resolved the problem. The JS message handler still needs to be in the viewDidLoad.

Example:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        webView.configuration.userContentController.add(self, name: "jsHandler")
    }

    override func viewDidAppear(_ animated: Bool) {
        //webView.configuration.userContentController.add(self, name: "jsHandler")
        let bundleURL = Bundle.main.resourceURL!.absoluteURL
        let html = bundleURL.appendingPathComponent("index.html") //Loads internal HTML files
        webView.loadFileURL(html, allowingReadAccessTo:bundleURL)
        webView!.uiDelegate = self
        webView.allowsBackForwardNavigationGestures = true //Allows 'safari' style gestures swipe back etc
    }
128K
  • 63
  • 1
  • 7

2 Answers2

2

Try something like this on each tab action:

func reloadTabAction() {
    if let url = webView.url {
        webView.reload()
    } else {
        webView.load(URLRequest(url: originalURL))
    }
}
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
  • Thanks for replying. I have 2 questions on this. 1. Where do I add this function as I do not have any tab actions. 2. Is there an advantage to having an if statement here? thanks – 128K Jul 26 '19 at 11:52
  • 1
    1. If you do not have action but on selecting the tabs if `viewWillAppear(_ animated: Bool)` fires you can add my code into `viewWillAppear(_ animated: Bool)` 2. some times web page fail to load offline, then `webView.url` be `nil`, in such case you should check if url is present then reload your webView – Anupam Mishra Jul 26 '19 at 11:56
1

We have reload property for WKWebView. So you can directly call the method. You can call the method while tapped the tabView Try the below code,

webView.reload()
Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43
  • 1
    Thanks for replying. This didn't work in the viewWillAppear method, am I putting it in the wrong place? – 128K Jul 26 '19 at 11:54