IPhone UIWebView loadHtmlString not added to history

My UIWebview loads the local html file using loadHtmlString. The loaded page has links to other local html files, as well as to real links that use the Internet.

I added a back button:

if ([self.webView canGoBack]) [self.webView goBack];

This works great, except that it does not recognize the original page loaded by loadHtmlString.

For example, if I move:
local -> local -> website local X local <- web (first reverse work, next does nothing.)

How can I get a webview to recognize the original page so that the back button also works on it? Can I add it to my web browsing history?

Thanks in advance!

+6
source share
3 answers

I had the same problem. I tried to manage the story, but it is error prone. Now I have found a better solution to this.

What you want to do is simply add loadRequest roughly: blank and do it as a placeholder for you before you call loadHTMLString / loadData. Then you are completely free from monitoring history. Webview.canGoBack and canGoForward will work. Of course, you will need to hack the handle to return to the placeholder: empty. You can do this in webViewDidFinishLoad. Here is the code:

In a function when calling loadHTMLString:

 [weakSelf.fbWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]]; [weakSelf.fbWebView loadHTMLString:stringResponse baseURL:url]; 

Code to handle goBack:

 - (void)webViewDidFinishLoad:(UIWebView *)webView { if ([webView.request.URL.absoluteString isEqualToString:@"about:blank"] && ![webView canGoBack] && [webView canGoForward]) { [weakSelf.fbWebView loadHTMLString:stringResponse baseURL:url]; } } 

I think it is also possible to expand this solution to handle these loadHTMLString, which are not the first load. Just having a Stack to write the entire line response and paste about: blank on each loadHTMLString. And put the stack each time back to roughly: empty.

+6
source

In this workaround, I used:

I created a delegate for WebView that kept track of whether the user was on the first page (loaded using loadHtmlString ).

When the back button is pressed, and WebView can no longer go back, and the user is not on the first page, the first page is loaded again (using loadHtmlString ). Here is the relevant code:

 if ([self.webView canGoBack]) [self.webView goBack]; else { WebViewDelegate * customDel = (WebViewDelegate *) self.webView.delegate; if (customDel.onFirstPage == NO) { // Load original local page. [self.webView loadHTMLString:self.htmlStr baseURL:[[NSBundle mainBundle] bundleURL]]; customDel.onFirstPage = YES; } } 

I will not select this answer for a while if a more elegant solution appears.

Hope this helps someone!

+2
source

as Cloud Xu said you just need to preload the dummy html, for example:

  webView.load(URLRequest(url: URL(string: "about:blank")!)) webView.loadHTMLString("<html><body><b>Your page data</b></body></html>", baseURL: nil) 

only this and nothing more! Swift3

+1
source

All Articles