iPhone UIWebView loadHtmlString未添加到历史记录 [英] iPhone UIWebView loadHtmlString not added to history

查看:160
本文介绍了iPhone UIWebView loadHtmlString未添加到历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UIWebview使用loadHtmlString加载本地html文件.加载的页面具有指向其他本地html文件的链接以及使用Internet的真实链接.

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:

如果([[self.webView canGoBack]) [self.webView goBack];

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

此方法工作正常,但无法识别装载有loadHtmlString的原始页面.

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

例如,如果我导航:
本地->本地->网络
local X local<-web(第一个背面起作用,第二个背面不起作用.)

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

我如何才能使Web视图识别原始页面,因此后退按钮也适用于该页面?我可以通过某种方式将其添加到网络视图的历史记录中吗?

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?

提前谢谢!

推荐答案

我遇到了同样的问题.我曾尝试管理历史记录,但是容易出错.现在,我发现了一个更好的解决方案.

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

您想要做的就是简单地将loadRequest添加到about:blank,并在调用loadHTMLString/loadData之前将其作为占位符.然后,您完全无需监视历史记录. webview.canGoBack和canGoForward就可以了.当然,您将需要一个hack来处理有关占位符:blank的信息.您可以在webViewDidFinishLoad中执行此操作.这是代码亮点:

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:

在调用loadHTMLString的函数中:

In the function when you call loadHTMLString:

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

处理goBack的代码:

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

我认为也有可能扩展此解决方案来处理那些不是第一次加载的loadHTMLString.只需通过堆栈来记录所有字符串响应,然后在每个loadHTMLString上插入about:blank.每次回到about:blank时,弹出堆栈.

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.

这篇关于iPhone UIWebView loadHtmlString未添加到历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆