1

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];



}

谢谢

4

1 回答 1

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];
于 2011-02-06T06:08:06.353 回答