ios UIWebView 网页缓存以供离线查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16826820/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:43:31  来源:igfitidea点击:

UIWebView webpage caching for offline viewing

iosobjective-cuiwebviewnsurlrequest

提问by jovanjovanovic

First of all, I'm pretty sure that I have checked every answer here and nothing does what I would like to do.

首先,我很确定我已经检查了这里的每个答案,但没有做我想做的事情。

  1. In this question, for answer is given ASIHTTPRequest which is dead project. (How do I download an entire webpage (with images) on the iPhone?)
  2. In this question, user proposed RNCachingURLProtocol which is really great but I had a few problems after closing app completely (closing it in task-bar). After that I didn't get css or images, only html was loaded. (Cache a single webpage for use when offline in Xcode / UIWEBVIEW).
  1. 在这个问题中,给出的答案是 ASIHTTPRequest,这是一个死项目。(如何在 iPhone 上下载整个网页(带图片)?
  2. 在这个问题中,用户提出了 RNCachingURLProtocol 这真的很棒,但是在完全关闭应用程序(在任务栏中关闭它)后我遇到了一些问题。之后我没有得到 css 或图像,只加载了 html。(缓存单个网页以在 Xcode / UIWEBVIEW 离线时使用)。

There are few more answers but none is good. There must be some simple implementation for what I'm searching.

还有几个答案,但没有一个是好的。我正在搜索的内容必须有一些简单的实现。

I would like to: When app opens, it loads some webpage. I want to save that webpage completely. Now user can quit or do whatever he wants (just not uninstall). As long as there is some internet connection (I check that using reachability class), webpage loads normally and it's being saved. IF USER opens app and there is NO INTERNET connection I just want to show message that "it might not be up to date bla bla boa" and show complete, saved webpage that was saved last time application has internet connection.

我想:当应用程序打开时,它会加载一些网页。我想完全保存那个网页。现在用户可以退出或做任何他想做的事情(只是不卸载)。只要有一些互联网连接(我使用可达性类检查),网页就会正常加载并被保存。如果用户打开应用程序并且没有互联网连接,我只想显示“它可能不是最新的 bla bla boa”的消息,并显示上次应用程序有互联网连接时保存的完整、保存的网页。

What would be the best way (up to date) to save complete webpage. I'v found something about MKNetworkKit but I'm not sure how to use that. Any help would be appreciated.

保存完整网页的最佳方式(最新)是什么。我发现了一些关于 MKNetworkKit 的信息,但我不确定如何使用它。任何帮助,将不胜感激。

采纳答案by Edwin Vermeer

It sounds like the standard caching is not good enough because you have no control over what will be cached and for how long. The easiest way for solving this is by creating your own caching meganism by overriding the NSURLCache. You can find some documentation about that at http://nshipster.com/nsurlcache/and a sample at http://github.com/evermeer/EVURLCacheThat sample even let you use a pre populated cache that can be included in your app install.

听起来标准缓存不够好,因为您无法控制缓存的内容和时间。解决这个问题的最简单方法是通过覆盖 NSURLCache 来创建自己的缓存机制。您可以在http://nshipster.com/nsurlcache/ 上找到一些关于此的文档,在http://github.com/evermeer/EVURLCache 上找到一个示例该示例甚至允许您使用可以包含在您的应用程序中的预填充缓存安装。

回答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];

The URL loading system provides a composite on-disk and in-memory cache of responses to requests. This cache allows an application to reduce its dependency on a network connection and increase its performance.

URL 加载系统提供对请求的响应的复合磁盘和内存缓存。此缓存允许应用程序减少对网络连接的依赖并提高其性能。