here is my code, it tries to download a webpage using NSURLDownload. but it does not work, its a command line program.
- (void)startDownloadingURL
{
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/index.html"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// Create the download with the request and start loading the data.
NSURLDownload *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
if (theDownload) {
// Set the destination file.
[theDownload setDestination:@"/saleh" allowOverwrite:YES];
} else {
// inform the user that the download failed.
NSLog(@"download has failed!");
}
}
Ensure you implement the downloadDidFinish and doanload:didFailWithError: callbacks.
Here’s an overview that covers how to use NSURLDownload:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-BAJEAIEE
Specifically, this callback will give you more details on why it failed:
EDIT:
Below you said it’s a command line. async callbacks require an NSRunLoop. See:
Cocoa: NSURLConnection not attempting an HTTP Request
Per the docs:
initWithRequest:delegate:
Returns an initialized URL download for a URL request and begins to download the data for the request.
NSURLConnection has a synchronous way to download data.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE
The main limitation is the client blocks but that’s a concern in a command line app.