views:

167

answers:

1

Hello everyone..

I need some help in using the NSURLCache to download a simple file. It would need to be saved in the Documents folder of the app. I have seen the URLCache example that apple presented, but I didn't find it useful since it was an image that was being shown. In addition there was some complex code on the modification date. I actually need something when the application opens it will begin the download process. Can someone guide me through just the download process of any file?

Thanks

Kevin

+1  A: 
NSURL *myURL = [NSURL urlWithString:@"http://www.google.com/theInternet.zip"];
NSData *myData = [NSData dataWithContentsOfURL:myURL];

[myData writeToFile:@"ThePath" atomically:YES];

This will block, but should be ok for small downloads. If you are going to be downloading something that is many megabytes then look into asynchronous downloads. You can see an example here:

http://github.com/erica/iphone-3.0-cookbook-/tree/master/C13-Networking/07-Asynchronous%20Downloads/

Steve Melvin
I don't believe the code given works... I can't see the html file I downloaded in the "Documents" folder of the application.
Kevin
You need to replace ThePath with the correct path. If you want to write to the documents folder use:[NSHomeDirectory() stringByAppendingString:@"/Documents"];
Steve Melvin