NSTask不起作用;我认为这与论点有关。这是我的代码:
- (IBAction)downloadFile:(id)sender {
// allocate our stuff :D
progressIndication = [[NSProgressIndicator alloc] init];
NSTask *downloader = [[NSTask alloc] init];
// set up the downloader task
[downloader setLaunchPath:@"/usr/bin/curl"];
[downloader setArguments:[NSArray arrayWithObject:[NSString stringWithFormat:@"-LO %@", downloadURL]]];
// go to the Desktop!
system("cd ~/Desktop");
// start progress indicator
[progressIndication startAnimation:self];
// download!
[downloader launch];
// stop the progress indicator, everything is done! :D
[progressIndication stopAnimation:self];
}
由于
答案 0 :(得分:3)
您真的不需要使用curl
来执行此操作;只需使用NSData
就可以更轻松地完成任务:
NSData *data = [NSData dataWithContentsOfURL:downloadURL];
[data writeToFile:[[NSString stringWithFormat:@"~/Desktop/%@", [downloadURL lastPathComponent]] stringByExpandingTildeInPath] atomically:YES];
如果您坚持要为此使用curl
,那么您将不得不修复您的代码,但由于多种原因这些代码无效。首先,你的论点是错误的。您应该拥有以下代码:
[downloader setArguments:[NSArray arrayWithObjects:@"-L", @"-O", [downloadURL absoluteString], @"-o", [NSString stringWithFormat:@"~/Desktop/%@", [downloadURL lastPathComponent]], nil];
其次,system("cd ~/Desktop")
毫无意义;摆脱它。
最后,NSTask
同时运行。 [downloader launch]
启动操作,您的代码继续。它应该是:
[downloader launch];
[downloader waitUntilExit]; // block until download completes
[progressIndication stopAnimation:self];