Html UIWebView 中的 Html5 缓存清单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1540240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Html5 cache manifest in a UIWebView?
提问by Tyler
I'd like to be able to use the html5 cache manifest to store images locally on an iPhone that is visiting the page via a UIWebView
within an app.
我希望能够使用 html5 缓存清单UIWebView
在通过应用程序内访问页面的 iPhone 上本地存储图像。
I've set up a sample that I think conforms to the specs, and appears to work in safari 4 and mobile safari, but not in my app's UIWebView
.
我已经设置了一个我认为符合规范的示例,并且似乎在 safari 4 和移动 safari 中工作,但在我的应用程序的UIWebView
.
The sample html is set up at http://bynomial.com/html5/clock3.html
.
示例 html 设置为http://bynomial.com/html5/clock3.html
。
This is very similar to the sample provided in the HTML5 draft standard
.
这与 中提供的示例非常相似HTML5 draft standard
。
Here is the entire (non-template) code of the sample app I'm using for testing:
这是我用于测试的示例应用程序的整个(非模板)代码:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// I thought this might help - I don't see any difference, though.
NSURLCache* cache = [NSURLCache sharedURLCache];
[cache setDiskCapacity:512*1024];
CGRect frame = [[UIScreen mainScreen] applicationFrame];
UIWebView* webView = [[UIWebView alloc] initWithFrame:frame];
[window addSubview:webView];
NSString* urlString = @"http://bynomial.com/html5/clock3.html";
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[window makeKeyAndVisible];
}
I've reviewed a few related questions on stackoverflow, but they don't seem to provide info to solve this. For example, I'm pretty sure the files I'm trying to cache are not too large, since they are just a couple small text files (way < 25k).
我已经查看了有关 stackoverflow 的一些相关问题,但它们似乎没有提供解决此问题的信息。例如,我很确定我尝试缓存的文件不会太大,因为它们只是几个小的文本文件(小于 25k)。
Any ideas for how to get this to work?
有关如何使其工作的任何想法?
回答by Mark Lummus
I got this to work using a .manifest file in a UIWebView. I discovered the trick on the Apple developer forums.
我在 UIWebView 中使用 .manifest 文件让它工作。我在 Apple 开发者论坛上发现了这个技巧。
you must deliver the proper mime-type for the manifest file: it must be of type "text/cache-manifest" - if it is anything else, then you won't get your files cached.
您必须为清单文件提供正确的 MIME 类型:它必须是“文本/缓存清单”类型 - 如果是其他类型,则不会缓存文件。
you can use web-sniffer at http://web-sniffer.net/to look at the headers returned by your server.
您可以使用http://web-sniffer.net/上的web-sniffer查看服务器返回的标头。
if you are using .htaccess files on your web server, then add the following line to the .htaccess file:
如果您在 Web 服务器上使用 .htaccess 文件,则将以下行添加到 .htaccess 文件中:
AddType text/cache-manifest .manifest
make sure to clear your browser cache after making this change to see the effect.
确保在进行此更改后清除浏览器缓存以查看效果。
HTH Mark
HTH标志
edit: this is only supported on iOS 4.0 and later. You will need to implement your own caching mechanism for iOS 2.x and 3.x :-(
编辑:这仅在 iOS 4.0 及更高版本上受支持。您需要为 iOS 2.x 和 3.x 实现自己的缓存机制:-(
回答by Eoin
回答by Alex Wayne
UIWebView
's cannot (or will not) use the HTML5 appcache. They just don't. Maybe in iPhone OS 4.0.
UIWebView
不能(或不会)使用 HTML5 appcache。他们只是没有。也许在 iPhone OS 4.0 中。
But what you can do is fetch all the files yourself with network calls (or just have them in your app bundle) and load up the webview with a local URL pointing to your local HTML file, which can have relative URLs to local assets.
但是您可以做的是通过网络调用自己获取所有文件(或者只是将它们放在您的应用程序包中)并使用指向本地 HTML 文件的本地 URL 加载 webview,该文件可以具有本地资产的相对 URL。
There is no super simple "it just works" way to make this happen, however.
然而,没有超级简单的“它只是有效”的方式来实现这一点。
回答by William Niu
Have you tried to create a customised NSURLCache object and use the setSharedURLCache: method, as recommended in the documentation:
您是否尝试按照文档中的建议创建自定义 NSURLCache 对象并使用 setSharedURLCache: 方法:
"Applications with more specific needs can create a custom NSURLCache object and set it as the shared cache instance using setSharedURLCache:"
“具有更多特定需求的应用程序可以创建自定义 NSURLCache 对象并使用 setSharedURLCache 将其设置为共享缓存实例:”
回答by Mark
I was experiencing the same problem. Although I used:
我遇到了同样的问题。虽然我用过:
[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0];
It didn't seem to want to use the cached data. Meaning it constantly made the request to my webservice to retrieve the latest content and thus made caching useless.
它似乎不想使用缓存的数据。这意味着它不断向我的网络服务发出请求以检索最新内容,从而使缓存变得无用。
I did find a work-around. When loading the URL in the UIWebView use the following code:
我确实找到了解决方法。在 UIWebView 中加载 URL 时使用以下代码:
NSString *html = [NSString stringWithFormat:@"<html><head><script type=\"text/javascript\">location.href='%@';</script></head><body></body></html>", [[currentURLRequest URL] absoluteString]];
[self.webView loadHTMLString:html baseURL:nil];
This has been tested on the OS 4.2, since I use the manifest to handle the caching (HTML5). There is a big chance that it won't work on OS 2.x/3.x, thou this needs to be confirmed.
这已经在 OS 4.2 上进行了测试,因为我使用清单来处理缓存 (HTML5)。它很可能无法在 OS 2.x/3.x 上运行,这需要确认。
The cons about using this method is that you can't send any POST data with your request.
使用此方法的缺点是您不能随请求发送任何 POST 数据。
Hope this helps anyone.
希望这可以帮助任何人。
You still need to send the request with 'cachePolicy:NSURLRequestReturnCacheDataElseLoad' else it will still not work.
您仍然需要使用 'cachePolicy:NSURLRequestReturnCacheDataElseLoad' 发送请求,否则它仍然无法工作。