Passing multiple arguments to NSTask is a troublesome when up-scaling, for example, passing the below to NSTask with quotes and backslash's to escape the special characters, will fail.
networksetup -getsecurewebproxy Wi-Fi | awk -F ': ' '{ print $2 }' && networksetup -getwebproxy Wi-Fi | awk -F ': ' '{ print $2 }
however if you add the code into a shell script, and run the shell script with NSTask you will not have a problem.
.h file
@property (nonatomic, strong) __block NSTask *buildTask;
@property (nonatomic, strong) NSPipe *outputPipe;
.m file
NSMutableArray *arguments = [[NSMutableArray alloc] init];
[arguments addObject:@"Wi-FI"];
[arguments addObject:@"off"];
//location of script to run
NSString * locationOfScript = [[NSBundle mainBundle] pathForResource:@"wifi" ofType:@"sh"];
self.buildTask = [[NSTask alloc] init];
self.buildTask.launchPath = locationOfScript; //location of wifi script to run
self.buildTask.arguments = arguments;
// Output Handling
self.outputPipe = [[NSPipe alloc] init];
self.buildTask.standardOutput = self.outputPipe;
[[self.outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
[self.buildTask launch];
[self.buildTask waitUntilExit];
if ([self ->_buildTask terminationStatus] == 0) {
//Confirms NSTask completes correctly.
}
Create a new file in xcode project add it you your project with the following.
#!/bin/sh
networksetup -setwebproxystate Wi-Fi off
you will need to use the terminal to chmod +x wifi.sh, otherwise you will receive an error (file not found)
The NSMutableArray's arguments will be passed to the shell script in order and can be accessed with "${1}" being @"Wi-Fi" in the example above.
In regards to your other question, you cannot bypass the requirement for the Administrator password.
you can add the command to the sudoers file and the password will not be asked, however this method is not practical for production apps.
with your Socks question, the easiest method would be to create a new file in project name it socks.sh and you can you either use;
[-setsocksfirewallproxy networkservice domain portnumber authenticated username password]
[-setsocksfirewallproxystate networkservice on | off]
For more info about networksetup and available options can be found in the networksetup man page (linked below)
[link]https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/networksetup.8.html