1

I have a single wkwebview which I will switch between tabs to load different urls say, URLl1 , url 2 and URL 3.

Consider the scenario:

  1. In the first tab with URL 1, Click on the link or do something and go to second tab with URL 2.

  2. When coming back to the first tab with URL 1,I need to load the web page exactly from where I left off.

1 Answers1

1

Architecturally it would make more sense to assign a WKWebView for each tab, and load the relevant URLs when you change tabs. You can intercept the navigation using :

(void)webView:(WKWebView *)webView 
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction 
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;

And get the request from the WKNavigationAction object. Use the request to load urls in another WKWebView.

Let each WKWebView manage its own history stack. Cookies should be shared across the WKWebViews as well, so using multiple WKWebView should not present a problem.

Michael
  • 3,269
  • 2
  • 23
  • 16
  • My intention is not to use each webview for every tab as we may have more and more tabs. Also I do not wish to load the URL everytime I switch tab. Is there any way at all to cache or save the webarchive completely and load it from local when we come back by clicking same tab again – Shalini Padu May 20 '17 at 08:50
  • WKWebView is designed to be used in the manner I specified above. It has its own process and can be dumped from memory by the system automatically if its a memory hog. As for caching the web page and reloading, sure you could save and reload the entire page, but why? When you return to a prior tab the prior web content will still be there. – Michael May 21 '17 at 14:55
  • Thanks. I understood your point. For the other way, as you mentioned, what is the best way to cache the webpage and reload it. – Shalini Padu May 21 '17 at 16:09
  • It's typically the best policy to use built in mechanisms for caching. https://developer.apple.com/reference/foundation/nsurlrequestcachepolicy?language=objc – Michael May 22 '17 at 18:36
  • Alternatively if you want to grab the page HTML using a WKUserScript and load it again with loadHtmlString you could try and follow the example here https://stackoverflow.com/questions/34751860/get-html-from-wkwebview-in-swift – Michael May 22 '17 at 18:42
  • You asked me (via a different question) to look at this one. I agree with Michael on this issue. What you're trying to do is actually fairly complicated and you're better off relying on Apple's code to get the job done. You don't have months to work out all the details, do you? I think you should accept his answer and open a new question if you still have doubts. – Mozahler May 25 '17 at 14:39