views:

83

answers:

4

Hi,

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

A: 

wget for iPhone.

buckbova
Sorry, as I want to publish my app on the App Store, I can't use jail breaks. ;-)
Stefan
A: 

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

Hope this clears up your confusion, Lee.

Lee Brooks
Wrong, you can save the source-code and later use that in a UIWebView.
Emil
+1  A: 

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];
Emil
Not a down voter, but I think he asked for downloading a whole website, not just a single HTML page. You might consider updating your answer to add info about parsing the HTML, pulling the links into a queue and looping it. Any solution that doesn't require a jailbreak is better than mine IMHO.
Byron Whitlock
Hm, he wrote *one HTML-site*, so I don't think he meant a whole website. That's not what I thought of when reading his question, anyway :)
Emil
And he also didn't say anything about doing it from an existing app, or creating his own. So, since SO is a **coding** -website, I obviously though of a code-solution.
Emil
Emil, I voted your answer up. And I know how to download a single page. But the core of my question is how to crawl and download all the tiny parts of a website in order to save it. Regards, Stefan
Stefan
Ah, ok. Maybe I'll edit my answer if I find something relevant on this.
Emil
Ok, Regex parsing sounds like a good idea. I will try to find a list of things, I need to parse. Thanks
Stefan
You most likely only need to parse the things I wrote here, scr= is used for images and iFrames (about the only things that need to be stored) and stylesheets of course. There may be more, so it's probably a good idea to try to find out more about what you need to parse. You said earlier that you might want to download all subitems of the file, but I don't think that will work, images and stylesheets may be located in folders above the file, or even on other websites. I think this is the approach you should be using.
Emil
A: 

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.

Byron Whitlock
Sorry, as I want to publish my app on the App Store, I can't use jail breaks. ;-)
Stefan
@Stefan You should probably have mentioned that in your post, people got confused :)
Emil
I updated my post. Thanks for your answers.
Stefan
@Byron Why I want to do this mobile? To allow users to save a website (to be precise: a web page) locally for viewing it offline. I don't want to crawl the whole website with every subpage. I only want to crawl one page for all subfiles and subfolders.
Stefan