UIWebView和NSURLCache有一个混乱的关系 [英] UIWebView and NSURLCache have a troubled relationship

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

问题描述

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

I am trying to solve 2 things in my UIWebView:


  1. 加载在适当的条件下代替远程资产的本地资产

  2. 如果没有本地资产或者它们已经过时,则正常加载远程资产

  3. 允许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 设置正确。

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.


  1. UIWebView 忽略服务器 Cache-Control HTML内容标题

  2. UIWebView 要求提供 NSCachedURLResponse 用于主HTML请求,但随后被忽略并丢弃。

  3. UIWebView 要求资产 NSURLCache ,但是当加载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有一个混乱的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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