本文介绍了为什么iOS7中的UIWebView canGoBack=NO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将这个网站嵌入到我的应用中,如下所示:

I'm embedding this web site into my app like this:

NSString *url = [NSString stringWithFormat:@"https://mobile.twitter.com/search?q=%@", @"@test OR #test"];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self.twitterWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

self.twitterWebView.scalesPageToFit = YES;

我有 2 个按钮用于在此网站中返回和前进.我在打电话

And I have 2 buttons for going back and forward in this web site. I'm calling

[self.twitterWebView goBack];
[self.twitterWebView goForward]; 相应地.

这在 iOS 6 上运行良好,但在 iOS 7 上,我的 web 视图的 canGoBack 和 canGoForward 属性为 NO,因此我的后退和前进按钮不起作用.

This works fine on iOS 6 but on iOS 7, my web view's canGoBack and canGoForward properties are NO and thus my back and forward buttons do not work.

附带说明,当第一次安装应用程序并且第一次加载页面时,我的按钮可以工作.但是当我再次运行我的应用程序时,当我点击网站上的链接时,我的 web 视图的 canGoBack 属性开始总是返回 NO.

As a side note, when the app is installed the first time, and the page is loaded the first time, my buttons work. But when I run my app again, and when I tap on a link on the web site, my web view's canGoBack property begins returning always NO.

我该如何解决这个问题?

How can I solve this?

编辑:我上传了一个迷你测试应用来演示我的问题.您可以从这里下载.请在 iOS 7 模拟器上运行应用程序,第一次安装应用程序时,请查看后退按钮是否正常工作.然后退出,再次运行应用程序,你会看到它会停止工作.

EDIT: I uploaded a mini test app that demonstrates my problem. You can download it from here. Please run the app on an iOS 7 simulator, see that the back button is working on the first installation of the app. Then quit, run the app again and you'll see that it'll stop working.

顺便说一句,问题似乎与 twitter 移动网站有关.你可以试试其他网址看看.

By the way the problem seems to be about the twitter mobile site. You can try another web site address and see that.

推荐答案

这似乎与 HTML5 的有关应用程序缓存"功能.首次启动时,该站点未缓存,并且 UIWebView 正确检测它是否可以前进或后退.一旦缓存被填充,新的 UIWebView 实例会决定,即使 URL 发生变化(可以在 UIWebViewDelegatewebView:shouldStartLoadWithRequest:navigationType:),前进或后退不再可能.canGoForwardcanGoBack 将返回 NO 并且 goForwardgoBack 不会做任何事情.只要此特定网站的 HTML5 缓存存在,这种情况在应用重新启动后仍然存在.

This seems to be related to HTML5's "Application Cache" functionality. On first launch, the site isn't cached and the UIWebView correctly detects if it can go forward or back. As soon as the cache is populated, new UIWebView instances decide that, even if the URL changes (which can be observed in UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType:), going forward or back is not possible anymore. canGoForward and canGoBack will return NO and goForward and goBack won't do anything. This persists across restarts of the app, as long as the HTML5 cache for this specific site exists.

也许此问题仅限于修改 URL 的 哈希标记后的片段标识符的网络应用通过 JavaScript.是的,UIWebView 在这种情况下的行为在 iOS 6 和 iOS 7 之间确实发生了变化.

Maybe this problem is limited to web apps that modify the URL's Fragment identifier after the hashmark via JavaScript.And yes, the UIWebView's behavior in this situation DID change between iOS 6 and iOS 7.

我还没有找到解决方案,我们可能不得不等待 Apple 在 iOS 7.1 左右解决这个问题.

I haven't found a solution yet, and we'll probably have to wait for Apple to fix this in iOS 7.1 or so.

编辑

其他人也有这个问题:

如果您正在使用应用程序缓存并管理通过散列或其他技术状态,历史对象不会保留您的导航历史记录,因此 history.back() 将永远无法工作并且 history.length 永远保持在 1 中.

(来自 http://www.mobilexweb.com/blog/safari-ios7-html5-problems-apis-review)

编辑 2

Safari 7.0(9537.71,OS X 10.9 Mavericks 中的默认设置)中也存在此问题.但是,最新的 WebKit nightly build (r158339) 似乎可以正常工作.修复将其发布到 iOS 和 OS X 版本很可能只是时间问题.

This problem exists in Safari 7.0 (9537.71, default in OS X 10.9 Mavericks), too. However, the most recent WebKit nightly build (r158339) seems to work correctly. It's most likely only a matter of time until the fix makes it to an iOS and OS X release.

编辑 3

这个问题在 iOS 7.1 和 OS X 10.9.2 中仍然存在.

This problem still exists in iOS 7.1 and and OS X 10.9.2.

编辑 4

此错误已在 iOS 8 和 OS X 的 Safari 7.1 (9537.85.10.17.1) 中修复!

This bug has been fixed in iOS 8 and Safari 7.1 (9537.85.10.17.1) for OS X!

相关:

这篇关于为什么iOS7中的UIWebView canGoBack=NO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:52