Shell Script from Objective c

2019-08-09 01:51发布

问题:

How can i run following command from objective using NSTask,

pluginkit -e ignore -i com.xxxxx.Plugin_Id

I tried this but not worked,

NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/bin/sh"];
   [task setArguments: @[@"pluginkit",@"-e",@"ignore",@"-i",@"com.xxxxx.Plugin_Id"]];
    [task launch];

回答1:

setLaunchPath should be the binary you launching. pluginkit is located on /usr/bin/, so the launch code should be like this i think:

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/pluginkit"];
[task setArguments: @[@"-e", @"ignore", @"-i", @"com.company.pluginId"]];
[task launch];
[task waitUntilExit];

or with a single lined call:

system("pluginkit -e ignore -i com.company.pluginId");