UIWebView中的Html5缓存清单?

时间:2022-12-10 08:07:20

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缓存清单在iPhone上本地存储图像,该iPhone通过应用程序内的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.

我已经设置了一个我认为符合规范的样本,并且似乎可以在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草案标准中提供的示例非常相似。

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

我已经回顾了一些关于*的相关问题,但它们似乎没有提供解决这个问题的信息。例如,我很确定我尝试缓存的文件不是太大,因为它们只是几个小文本文件(方式<25k)。

Any ideas for how to get this to work?

有关如何使其工作的任何想法?

5 个解决方案

#1


26  

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类型:它必须是“text / cache-manifest”类型 - 如果是其他任何内容,那么您将不会缓存您的文件。

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实现自己的缓存机制:-(

#2


3  

You could use the cachePolicy param on the NSURLRequest.

您可以在NSURLRequest上使用cachePolicy参数。

request = [NSURLRequest requestWithURL:[NSURL URLWithString:reqString] cachePolicy:NSURLRequestReturnCacheDataDontLoad timeoutInterval: 10.0];

documented here.

记录在这里。

#3


2  

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.

然而,没有超级简单的“它只是工作”的方式来实现这一点。

#4


1  

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将其设置为共享缓存实例:”

#5


1  

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.

它似乎不想使用缓存数据。这意味着它不断向我的web服务请求检索最新内容,从而使缓存无用。


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'发送请求,否则它仍然无法正常工作。

#1


26  

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类型:它必须是“text / cache-manifest”类型 - 如果是其他任何内容,那么您将不会缓存您的文件。

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实现自己的缓存机制:-(

#2


3  

You could use the cachePolicy param on the NSURLRequest.

您可以在NSURLRequest上使用cachePolicy参数。

request = [NSURLRequest requestWithURL:[NSURL URLWithString:reqString] cachePolicy:NSURLRequestReturnCacheDataDontLoad timeoutInterval: 10.0];

documented here.

记录在这里。

#3


2  

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.

然而,没有超级简单的“它只是工作”的方式来实现这一点。

#4


1  

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将其设置为共享缓存实例:”

#5


1  

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.

它似乎不想使用缓存数据。这意味着它不断向我的web服务请求检索最新内容,从而使缓存无用。


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'发送请求,否则它仍然无法正常工作。