hw-1

How do you use posix_spawn to replace the deprecated ‘system’ to launch opendiff in Objective-C?

How do you use posix_spawn to replace the deprecated ‘system’ to launch opendiff in Objective-C?

Using posix_spawn(), to answer your question:

1
2
3
4
5
6
7
8
9
10
11
12
#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);

Or, you could use NSTask:

1
2
3
4
5
6
7
8
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];