可可问题 - NSTask 不起作用

发布于 2024-10-15 20:13:03 字数 729 浏览 1 评论 0原文

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



}

谢谢

NSTask isn't working; I think it has to do with the arguments. Here is my code:

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



}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你的星星会说话 2024-10-22 20:13:03

您确实不需要使用 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];

You really don't need to use curl to do this; just use NSData to accomplish the task much more easily:

NSData *data = [NSData dataWithContentsOfURL:downloadURL];
[data writeToFile:[[NSString stringWithFormat:@"~/Desktop/%@", [downloadURL lastPathComponent]] stringByExpandingTildeInPath] atomically:YES];

If you insist you need to use curl for this, you're going to have to fix your code, which doesn't work for several reasons. First and foremost, your arguments are wrong. You should have the following code:

[downloader setArguments:[NSArray arrayWithObjects:@"-L", @"-O", [downloadURL absoluteString], @"-o", [NSString stringWithFormat:@"~/Desktop/%@", [downloadURL lastPathComponent]], nil];

Second, system("cd ~/Desktop") is meaningless; get rid of it.
Lastly, NSTask runs concurrently. [downloader launch] starts the operation, and your code continues. It should be:

[downloader launch];
[downloader waitUntilExit]; // block until download completes
[progressIndication stopAnimation:self];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文