简体   繁体   中英

iPhone: How to download a full website?

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

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

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

Regards,

Stefan

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

If last, you could simply do this:

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: For this approach, you would probably be best off using a regex-library for iPhone to parse through the string and find needed objects.

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.

Storing files:

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

Was looking for similar functionality and found this. Can't claim any credit for it, just wanted to make sure it was mentioned (as a drop-in solution) for people interested in it.

http://robnapier.net/offline-uiwebview-nsurlprotocol

  • Install wget on your jail broken iPhone
  • Use the spanning hosts options to download everything from the site.

    wget -rH -Dserver.com http://www.server.com/

But why do you want to do this on a mobile device? This is somthing that should be done on a real computer with lots of memory, disk space, bandwidth and multiple CPU cores.

这是Appstore链接https://itunes.apple.com/us/app/sitesucker/id346896838?mt=8该应用程序将整个网站原生下载到手机上。

You can't save websites to your phone, only view them (unless your jailbroken.)

Hope this clears up your confusion, Lee.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM