iPhone UIWebView loadHtmlString not added to history

白昼怎懂夜的黑 提交于 2019-12-09 16:12:17

问题


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

I have added a back button with:

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

This works fine except it does not recognise the original page loaded with loadHtmlString.

For example, if I navigate:
local -> local -> web
local X local <- web (The first back works, the next does nothing.)

How can I get the webview to recognise the original page so the back button also works for it? Can I add it to the webview's history somehow?

Thanks in advance!


回答1:


I had a same problem. I tried manage the history, but it is error prone. Now I have discovered a better solution of this.

What you want to do is simply add a loadRequest to about:blank and make that as a placeholder for you before you call loadHTMLString/loadData. Then you are totally free from monitoring the history. The webview.canGoBack and canGoForward will just work. Of course, you will need a hack to handle go back to the placeholder about:blank. You can do that in webViewDidFinishLoad. Here is the code highlight:

In the function when you call 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 expand this solution to handle those loadHTMLString that is not the first load. Just by having a Stack to record all the string response and insert an about:blank on each loadHTMLString. And pop the stack when each time go back to about:blank.




回答2:


as Cloud Xu said you just need to preload dummy html, like this:

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

just it and nothing else! Swift3




回答3:


Here's the workaround I ended up using:

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

When the back button is pressed, and the 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'll not choose this answer for a little while, in case a more elegant solution pops up.

Hopefully this helps someone!



来源:https://stackoverflow.com/questions/13754153/iphone-uiwebview-loadhtmlstring-not-added-to-history

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!