iPhone:如何下载完整的网站? [英] iPhone: How to download a full website?

查看:220
本文介绍了iPhone:如何下载完整的网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您建议我将下载网站(一个包含所有图片的HTML网站)到iPhone的方法?

what approach do you recommend me for downloading a website (one HTML site with all included images) to the iPhone?

问题是如何抓取所有这些位(Javascripts,图像,CSS)并保存在本地。这不是具体的实现(我知道如何使用NSURLRequest和东西,我正在寻找一个爬行/蜘蛛的方法)。

The question is how to crawl all those tiny bits (Javascripts, images, CSS) and save them locally. It's not about the concrete implementation (I know how to use NSURLRequest and stuff. I'm looking for a crawl/spider approach).

因为它是为一个官方(App Store)应用程序。

Jail breaks won't work, since it is intended for an official (App Store) app.

尊敬的,

Stefan

推荐答案

正在下载?或者获取网站的HTML源代码并用 UIWebView

Downloading? Or getting the HTML-source of the site and displaying it with a UIWebView?

显示它。这样做:

NSString *data = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://apple.com"] encoding:NSUTF8StringEncoding error:NULL];

// Load UIWebView with data
[webView loadHTMLString:data baseURL:[NSURL URLWithString:@"http://apple.com"]];

EDIT:
对于这种方法,最好使用一个regex-library for iPhone来解析字符串并找到所需的对象。

For this approach, you would probably be best off using a regex-library for iPhone to parse through the string and find needed objects.

你可以使用这个: RegexKitLite ,并使用几个Regex表达式来查找例如< link rel =%href = *> src =*。但是您必须记住存储它们并用新路径替换*的值。

You could use this: RegexKitLite, and do a couple of Regex-expressions to find, for example, <link rel="%" href="*"> and src="*". But you have to remember to store them and replacing the values of * with the new path.

存储文件

你将从正则表达式方法中获得url,你可以这样从url中写入文件:

You will get url's back from the regex-methods, and you can write the files from the url's like this:

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString pathToCurrentSite = [rootPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/", fullUrlToPage]];
for (urlString in urlStrings) {
    NSData *stringData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
    [fileManager createFileAtPath:[pathToCurrentSite stringByAppendingPathComponent:urlString] contents:stringData attributes:nil];
}
NSString *data;
NSData *pageData = [data dataUsingEncoding:NSASCIIStringEncoding];
[fileManager createFileAtPath:[pathToCurrentSite stringByAppendingPathComponent:@"index"] contents:pageData attributes:nil];
[fileManager release];

这篇关于iPhone:如何下载完整的网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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