Search code examples
objective-ciphonecocoaxcodensurlcache

URLCache - iPhone SDK


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


Solution

  • 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/