UIWebView 中的 Html5 缓存清单? [英] Html5 cache manifest in a UIWebView?

查看:20
本文介绍了UIWebView 中的 Html5 缓存清单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用 html5 缓存清单在通过应用内的 UIWebView 访问页面的 iPhone 上本地存储图像.

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.

我已经设置了一个我认为符合规范的示例,并且似乎可以在 safari 4 和移动 safari 中使用,但不能在我应用的 UIWebView 中使用.

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.

示例 html 设置在 http://bynomial.com/html5/clock3.html.

The sample html is set up at http://bynomial.com/html5/clock3.html.

这与 中提供的示例非常相似HTML5 标准草案.

这是我用于测试的示例应用的完整(非模板)代码:

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];
}

我已经查看了有关 stackoverflow 的一些相关问题,但它们似乎没有提供解决此问题的信息.例如,我很确定我要缓存的文件不会太大,因为它们只是几个小的文本文件(小于 25k).

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).

关于如何让它发挥作用的任何想法?

Any ideas for how to get this to work?

推荐答案

我使用 UIWebView 中的 .manifest 文件使其工作.我在 Apple 开发者论坛上发现了这个技巧.

I got this to work using a .manifest file in a UIWebView. I discovered the trick on the Apple developer forums.

您必须为清单文件提供正确的 MIME 类型:它必须是text/cache-manifest"类型 - 如果是其他类型,则不会缓存您的文件.

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.

您可以在 http://web-sniffer.net/ 使用 web-sniffer 来查看您的服务器返回的标头.

you can use web-sniffer at http://web-sniffer.net/ to look at the headers returned by your server.

如果您在 Web 服务器上使用 .htaccess 文件,则将以下行添加到 .htaccess 文件中:

if you are using .htaccess files on your web server, then add the following line to the .htaccess file:

AddType text/cache-manifest .manifest

确保在进行此更改后清除浏览器缓存以查看效果.

make sure to clear your browser cache after making this change to see the effect.

HTH标记

这仅在 iOS 4.0 及更高版本上受支持.您需要为 iOS 2.x 和 3.x 实现自己的缓存机制:-(

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 :-(

这篇关于UIWebView 中的 Html5 缓存清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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