UIWebView和NSURLCache有一个混乱的关系 [英] UIWebView and NSURLCache have a troubled relationship
问题描述
我正在尝试解决我的 UIWebView
中的两件事:
I am trying to solve 2 things in my UIWebView
:
- 加载在适当的条件下代替远程资产的本地资产
- 如果没有本地资产或者它们已经过时,则正常加载远程资产
- 允许webview要回而不需要重新加载它的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:
加载它们。瞧,看起来效果很好!我现在可以回去了,网络视图委托会拦截负载,并告诉它加载我手动提取的数据,而服务器不会被命中。
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 $ c加载数据或html字符串,它似乎只会要求缓存资产$ c>设置正确。
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.
尽管我付出了很多努力,但我无法使网页视图有条件地加载本地资产并导航回来而不会点击服务器。好像我在SDK中有一些bug。
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.
-
UIWebView
忽略服务器Cache-Control
HTML内容标题 -
UIWebView
要求提供NSCachedURLResponse
用于主HTML请求,但随后被忽略并丢弃。 -
UIWebView $ c如果你用一个请求加载它,$ c>要求资产
NSURLCache
,但是当加载HTML或数据时,它会完全绕过缓存并直接从远程服务器请求它。
UIWebView
ignores the serversCache-Control
header for HTML contentUIWebView
asks for aNSCachedURLResponse
's for the main HTML request but is then ignored and discarded.UIWebView
asks theNSURLCache
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有一个混乱的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!