UIWebView 和 NSURLCache 关系有问题

UIWebView and NSURLCache have a troubled relationship(UIWebView 和 NSURLCache 关系有问题)

本文介绍了UIWebView 和 NSURLCache 关系有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决 UIWebView 中的两件事:

I am trying to solve 2 things in my UIWebView:

  1. 在适当的条件下加载本地资源代替远程资源
  2. 如果没有本地资源或它们已过时,则照常加载远程资源
  3. 允许 web 视图返回"而不需要从服务器重新加载它的 html

为了处理#1 和#2,我实现了一个自定义子类NSURLCache.它在 webview 请求资产时被调用,而我可以发送本地的,中止服务器对资产的调用.我什至有一个小时间戳系统,它知道本地版本是否旧,以确定使用哪个版本.

So to handle #1 and #2, I implemented a custom subclass NSURLCache. It gets called when the webview requests assets, and I can send along the local ones instead, aborting the server call for the asset. I even have a little timestamping system that knows if the local version is old or not to figure out which one to use.

但是#3 让我陷入了循环.似乎不管缓存控制标头如何,UIWebView 总是会在返回时重新加载 HTML.

But #3 is throwing me for a loop. It seems that regardless of cache-control headers, UIWebView will ALWAYS reload the HTML when going back.

所以我尝试在我的自定义 NSURLCache 类中强制缓存 HTML.它似乎可以工作,并使用我想要的 HTML 返回 NSCachedURLResponse 的实例,但 UIWebView 没有注意到并从服务器加载它.经过一番搜索,我发现 UIWebView 和 url 缓存存在一些不一致",我想这是其中之一.对资产很有效,但对组成它试图显示的页面的 HTML 来说不是很好.

So I tried to force caching of the HTML in my custom NSURLCache class. It seems to work, and returns the instance of NSCachedURLResponse with the HTML I want, but the UIWebView fails to notice and loads it from the server anyway. After some searching I see there is some "inconsistencies" with UIWebView and the url cache, and I guess this is one. Works great for assets, but not so great for the HTML that makes up the page it's trying to display.

然后我决定,因为这可能行不通,所以手动进行.我创建了一堆 url,以及我手动获取并使用 loadData:MIMEType:textEncodingName:baseURL: 加载的数据.瞧,它似乎工作得很好!我现在可以回去了,Web 视图代理会拦截负载,并告诉它加载我手动获取的数据,这样服务器就不会被击中.

So then I decided that since this may not work, do it manually. I created a stack of urls, and their data that I fetch manually and load it with loadData:MIMEType:textEncodingName:baseURL:. Lo and behold it seemed to work great! I could now go back, the web view delegate would intercept the load, and tell it to load my manually fetched data instead, and the server would not be hit.

但是,我注意到 NSURLCache 不再用于我的资产.如果您使用 loadRequest: 加载内容而不是通过正确设置 baseUrl 加载数据或 html 字符串,它似乎只要求缓存资产.

However, then I noticed that the NSURLCache was no longer being used for my assets. It seems it only asks for cached assets if you load content with loadRequest: and not by loading data or an html string with the baseUrl set properly.

因此,尽管我付出了所有努力,但我还是无法让 Web 视图有条件地加载本地资产并在不访问服务器的情况下导航回来.看来我在 SDK 中有一些错误.

So despite all my efforts I cannot make a web view conditionally load local assets AND navigate back without hitting the server. It seems like I have a few bugs here in the SDK.

  1. UIWebView 忽略服务器的 Cache-Control HTML 内容标头
  2. UIWebView 为主 HTML 请求请求 NSCachedURLResponse,但随后被忽略并丢弃.
  3. UIWebViewNSURLCache 询问资产,如果您通过请求加载它,但当加载 HTML 或数据时,它会完全绕过缓存并直接从远程服务器.
  1. UIWebView ignores the servers Cache-Control header for HTML content
  2. UIWebView asks for a NSCachedURLResponse's for the main HTML request but is then ignored and discarded.
  3. UIWebView asks the NSURLCache for assets if you load it up with a request, but when loading with HTML or data it bypasses the cache completely and requests it directly from the remote server.

所以首先,有人找到解决这些问题的方法吗?其次,有什么理由说我很愚蠢而且这些实际上不是 SDK 错误?

So first up, anyone see a solution to these things? And second, any reason that I am being stupid and these are not actually SDK bugs?

推荐答案

我已经放弃了这个.有太多的陷阱.相反,我只是通过 ajax 加载新页面.而不是 [webview goBack] 我有一些自定义 javascript 可以在 [webview stringByEvaluatingJavascriptFromString:@"goBack()"]

I've abandoned this. There were just too many gotchas. Instead I just load new page in via ajax. And instead of [webview goBack] I have some custom javascript to do cool stuff for me on [webview stringByEvaluatingJavascriptFromString:@"goBack()"]

这篇关于UIWebView 和 NSURLCache 关系有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIWebView 和 NSURLCache 关系有问题

基础教程推荐