本文介绍了为什么iOS7中的UIWebView canGoBack = NO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在将此网站嵌入我的应用程序,如下所示: 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; 我在这个网站上有两个按钮可以前后移动。我正在打电话 [self.twitterWebView goBack]; 和 [self.twitterWebView goForward]; 相应。 这适用于iOS 6,但在iOS 7上,我的网页视图的canGoBack和canGoForward属性为NO,因此我的后退和前进按钮不起作用。 作为旁注,当第一次安装应用程序,并且第一次加载页面时,我的按钮会起作用。但是当我再次运行我的应用程序时,当我点击网站上的链接时,我的网络视图的canGoBack属性开始返回NO。 我该如何解决这个问题? 编辑:我上传了一个小测试应用程序,演示我的问题。您可以从此处下载。请在iOS 7模拟器上运行该应用程序,看到后退按钮正在处理应用程序的首次安装。然后退出,再次运行应用程序,你会发现它将停止工作。 顺便提一下,这个问题似乎与推特移动网站有关。您可以尝试其他网站地址并查看。解决方案这似乎与 HTML5的应用程序缓存功能。首次启动时,网站不会被缓存, UIWebView 会正确检测它是否可以前进或后退。一旦填充了缓存,新的 UIWebView 实例就会决定,即使URL发生了变化(可以在 UIWebViewDelegate 的 webView:shouldStartLoadWithRequest:navigationType: ),无法继续前进或后退。 canGoForward 和 canGoBack 将返回否和 goForward 和 goBack 将不会做任何事情。只要此特定网站的HTML5缓存存在,这在应用程序重新启动时会持续存在。 可能此问题仅限于修改URL 散列标记后的片段标识符。 是的, UIWebView 在这种情况下的行为DID在iOS 6和iOS 7之间变化。 修改 其他人也遇到此问题:(来自 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版本之前,这很可能只是时间问题。 编辑3 iOS 7.1和OS X 10.9.2中仍存在此问题。 编辑4 OS X中的iOS 8和Safari 7.1(9537.85.10.17.1)修复了此错误! 相关: 历史对象不保留导航历史记录ios7 safari 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;And I have 2 buttons for going back and forward in this web site. I'm calling[self.twitterWebView goBack]; and[self.twitterWebView goForward]; accordingly.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.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?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.By the way the problem seems to be about the twitter mobile site. You can try another web site address and see that. 解决方案 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.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.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.EditOther people have this problem, too:(from http://www.mobilexweb.com/blog/safari-ios7-html5-problems-apis-review)Edit 2This 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.Edit 3This problem still exists in iOS 7.1 and and OS X 10.9.2.Edit 4This bug has been fixed in iOS 8 and Safari 7.1 (9537.85.10.17.1) for OS X!Related:history object doesn't keep navigation history ios7 safari 这篇关于为什么iOS7中的UIWebView canGoBack = NO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 08:18