如何使用posix_spawn替换已弃用的“系统”以在Objective-C中启动opendiff? [英] How do you use posix_spawn to replace the deprecated 'system' to launch opendiff in Objective-C?
本文介绍了如何使用posix_spawn替换已弃用的“系统”以在Objective-C中启动opendiff?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这行代码:
system("/Applications/Xcode.app/Contents/Developer/usr/bin/opendiff /Users/LukeSkywalker/Documents/doc1.rtf /Users/LukeSkywalker/Documents/doc2.rtf");
给我这个警告:
'system' is deprecated: first deprecated in iOS 8.0 - Use posix_spawn APIs instead.
我已经阅读了一些关于posix_spawn的内容,但我无法弄清楚等价线是什么使用posix_spawn的代码看起来像。
I've read a little bit about posix_spawn, but I can't figure out what an equivalent line of code using posix_spawn would look like.
任何帮助或样本链接都将受到赞赏。
Any help or links to samples would be appreciated.
推荐答案
使用 posix_spawn()
,回答你的问题:
#include <spawn.h>
extern char **environ;
(...)
pid_t pid;
char *argv[] = {
"/Applications/Xcode.app/Contents/Developer/usr/bin/opendiff",
"/Users/LukeSkywalker/Documents/doc1.rtf",
"/Users/LukeSkywalker/Documents/doc2.rtf",
NULL
};
posix_spawn(&pid, argv[0], NULL, NULL, argv, environ);
waitpid(pid, NULL, 0);
或者,您可以使用NSTask:
Or, you could use NSTask:
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/Applications/Xcode.app/Contents/Developer/usr/bin/opendiff";
task.arguments = [NSArray arrayWithObjects:
@"/Users/LukeSkywalker/Documents/doc1.rtf",
@"/Users/LukeSkywalker/Documents/doc2.rtf",
nil];
[task launch];
[task waitUntilExit];
如果您不需要同步,只需删除对 waitpid()
(确保在其他地方调用它,或者你的应用程序退出之前你最终会使用僵尸进程)或 [task waitUntilExit]
。
If you don't need it to be synchronous, just remove the call to waitpid()
(make sure to call it somewhere else, or you'll end up with a zombie process until your app exits) or [task waitUntilExit]
.
这篇关于如何使用posix_spawn替换已弃用的“系统”以在Objective-C中启动opendiff?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文