Html 如何在 UIWebView 中缓存内容以便以后更快地加载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1348696/
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
How to cache content in UIWebView for faster loading later on?
提问by erotsppa
I notice that the iphone safari caches content so that your page load for later is much faster much like a desktop browser. So take mobile gmail web page for example, the first load is quite slow (5-10 secondS). But if I close the tab and reopen the page again, it's very quick (1 second).
我注意到 iphone safari 缓存内容,以便您以后的页面加载速度要快得多,就像桌面浏览器一样。所以以移动gmail网页为例,第一次加载很慢(5-10秒)。但是如果我关闭选项卡并再次重新打开页面,它会很快(1 秒)。
However, this behavior is not the same if you load the content through a UIWebView in your app. Am I missing some settings? How do I make the UIWebView cache the content automatically without going through the hassle of saving the content myself?
但是,如果您通过应用程序中的 UIWebView 加载内容,则此行为将有所不同。我错过了一些设置吗?如何让 UIWebView 自动缓存内容而不需要自己保存内容的麻烦?
回答by zaph
The key is: NSURLRequestReturnCacheDataElseLoad
关键是: NSURLRequestReturnCacheDataElseLoad
NSData *urlData;
NSString *baseURLString = @"mysite.com";
NSString *urlString = [baseURLString stringByAppendingPathComponent:@"myfile"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:nil];
if (connection)
{
urlData = [NSURLConnection sendSynchronousRequest: request];
NSString *htmlString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
[webView loadHTMLString:htmlString baseURL:baseURLString];
[htmlString release];
}
[connection release];
回答by Rahul K Rajan
NSString *stringurl=[NSString stringWithFormat:@"http://www.google.com"];
NSURL *url=[NSURL URLWithString:stringurl];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:15.0];
[uiwebview loadRequest:theRequest];
It will load a url first time then looks for for only the content changes..,if there is no updates in the url content it will load from the cache(local storage).
它将第一次加载一个 url,然后只查找内容更改..,如果 url 内容中没有更新,它将从缓存(本地存储)加载。
回答by fbrereto
Based on this discussion threadit would appear there isn't any OS-level caching possible with UIWebView
. Based on experience I've noticed that Safari on my iPhone OS device doesn't cache its web pages (e.g., hitting the back button in Safari does not reload the old page from a cache).
基于这个讨论线程,似乎没有任何操作系统级别的缓存可能与UIWebView
. 根据经验,我注意到 iPhone 操作系统设备上的 Safari 不会缓存其网页(例如,在 Safari 中点击后退按钮不会从缓存中重新加载旧页面)。
回答by Olie
I've done a couple of apps that cache pages to the Documents folder, then compare the time-stamps of the cached & web pages before loading the new web page. So the basic flow is:
我已经完成了几个将页面缓存到 Documents 文件夹的应用程序,然后在加载新网页之前比较缓存和网页的时间戳。所以基本流程是:
if (fileIsInCache)
if (cacheFileDate > webFileDate)
getCachedFile
else
getFileFromWeb
saveFileToCache
else
getFileFromWeb
saveFileToCache
stuffFileIntoUIView
maybeReduceCache
You still have to hit the web to get the headers, but that's typically much faster than downloading a whole page/image/file.
您仍然必须访问网络才能获取标题,但这通常比下载整个页面/图像/文件要快得多。