How can I run sudo commands from objective-c? I am able to run simple commands that don't require sudo with the following method:
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"];
NSArray *arguments = [NSArray arrayWithObjects:@"-c" ,[NSString stringWithFormat:@"%@", cmd], nil];
NSLog(@"run command: %@",cmd);
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *output;
output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
return output;
When I run a sudo command i get the error: Operation not permitted.
From what I found online I need the gain authorisation. I read the Authorisation documentation from Apple but I doesn't specify a stating point.
The example EvenBetterAuthorizationSample from Apple doesn't work.
The application is signed with an Apple ID.