Hi All,
I want to download a file to the downloads folder. I searched google for this and found the NSURLDownload class. I've read the page in the dev center and created this code (with some copy and pasting) this code:
@implementation Downloader
@synthesize downloadResponse;
- (void)startDownloadingURL:(NSString*)downloadUrl destenation:(NSString*)destenation {
// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:downloadUrl]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLDownload *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest
delegate:self];
if (!theDownload) {
NSLog(@"Download could not be made...");
}
}
- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename {
NSString *destinationFilename;
NSString *homeDirectory=NSHomeDirectory();
destinationFilename=[[homeDirectory stringByAppendingPathComponent:@"Desktop"]
stringByAppendingPathComponent:filename];
[download setDestination:destinationFilename allowOverwrite:NO];
}
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error {
// release the connection
[download release];
// inform the user
NSLog(@"Download failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)downloadDidFinish:(NSURLDownload *)download {
// release the connection
[download release];
// do something with the data
NSLog(@"downloadDidFinish");
}
- (void)setDownloadResponse:(NSURLResponse *)aDownloadResponse {
[aDownloadResponse retain];
[downloadResponse release];
downloadResponse = aDownloadResponse;
}
- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response {
// reset the progress, this might be called multiple times
bytesReceived = 0;
// retain the response to use later
[self setDownloadResponse:response];
}
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(unsigned)length {
long long expectedLength = [[self downloadResponse] expectedContentLength];
bytesReceived = bytesReceived+length;
if (expectedLength != NSURLResponseUnknownLength) {
percentComplete = (bytesReceived/(float)expectedLength)*100.0;
NSLog(@"Percent - %f",percentComplete);
} else {
NSLog(@"Bytes received - %d",bytesReceived);
}
}
-(NSURLRequest *)download:(NSURLDownload *)download
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse {
NSURLRequest *newRequest=request;
if (redirectResponse) {
newRequest=nil;
}
return newRequest;
}
@end
But my problem is now, it doesn't appear on the desktop as specified. And I want to put it in downloads and not on the desktop...
What do I have to do?