iOS缓存一个UIWebView [英] iOS Cache a UIWebView

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

问题描述

我已经编写了一个iOS应用程序,用于在 UIWebView 中加载 HTML5 web应用程序。我试图让应用缓存 UIWebView 中的所有内容在3G连接上使用,而不是加载它所有再次。

它需要遵循WiFi连接上的所有链接,并下载所有页面和图像。当在3G连接上时,它应该从缓存加载所有内容。



这可能吗?我应该从哪里开始的任何想法?是否需要让我的HTML5应用程序加载后台中的所有页面,而不是让应用程序执行它?

感谢您的帮助!


首先,我会使用Reachability来检测是否存在WIFI连接,并且您的服务器是可访问的。

解决方案

通过这种方式,您可以同样的方式处理3G连接和无互联网连接。 (访问缓存页面)。这也意味着你可以使用缓存,如果你有WIFI但仍然无法访问你的服务器。



我要么下载您的网页和所有相关资产(如果它是一个相当静态的网站)的zip文件,并将它们解压并存储在本地文件系统中。您可以在没有WIFI时将您的UIWebView指向本地目录。



您可以使用类似Zip Archive的东西来处理这个



ZipArchive GitHub页面



如果这不是一个选项,那么您可以在UIWebView中加载它时从页面获取HTML。这样做的问题是您依赖于用户访问页面以缓存它们。



您可以使用它获取HTML代码并将其存储在本地:

  //确定缓存文件路径
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * filePath = [NSString stringWithFormat:@%@ /%@,[paths objectAtIndex:0],@index.html];

//下载并写入文件
NSURL * url = [NSURL URLWithString:@http://www.google.co.uk];
NSData * urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];

//在UIWebView中加载文件
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

您也可以使用此代码在后台获取所有网页。要做到这一点,您需要硬编码所有必需的网址,并单独查看或更好地从网站上下载网页列表。

创建一个Web服务(JSON可能是您的最佳选择),并提供要下载的URL列表。然后,您可以通过这个JSON解析并使用这些URL来填充上面的代码并在本地缓存HTML。



请记住要注意超链接和任何链接图片和CSS,因为如果他们正在寻找完整的在线URL而不是相对路径,这将打破你的缓存网站。


I've written an iOS app that loads up an HTML5 web application inside a UIWebView. I'm trying to make the app cache everything in the UIWebView for use while on a 3G connection rather than load it all again.

It needs to follow all links while on a WiFi connection and download all pages and images. When on a 3G connection it should then load everything from the cache.

Is this possible? Any ideas on where I should start? Do I need to make my HTML5 application load all pages in the background rather than have the app do it?

Thanks for any help!

解决方案

Firstly I would use Reachability to detect if there is a WIFI connection and your server is accessible. This way you can handle both 3G connectivity and no internet connection in the same way. (accessing the cached pages). It also means you can use the cache if you have WIFI but still cannot reach your server.

Reachability sample code

I would either download a zip file of your web pages and all the associated assets (if it is a fairly static website) and unzip and store them in the local file system. You can then point your UIWebView to the local directory when there is not WIFI.

You could use something like Zip Archive to handle this

ZipArchive GitHub page

If this is not an option then you could get the HTML from the page when you load it in the UIWebView. The problem with doing this is you are reliant on the user accessing pages in order to cache them.

You can use this to get the HTML code and store it locally:

 // Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];   

// Download and write to file
NSURL *url = [NSURL URLWithString:@"http://www.google.co.uk"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];

// Load file in UIWebView
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

You could use this code to get all the web pages in the background as well. To do this you will either need to hard code all the required URLs and look them up individually or better still download a list of web pages from your site first.

You could create a web service (JSON is probably your best option for this) giving a list of URLs to download. You could then parse through this JSON and use the URLs to feed into the code above and cache the HTML locally.

Just remember to be careful with your hyperlinks and links to any images and CSS within your HTML as this will break your cached website if they are looking for your full online URL rather than a relative path.

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

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