Remove Helper Tool using NSTask in MAC Application

617 views Asked by At

I am working on mac application in which I am doing some stuff with root privilege and that is working fine and now I want to delete that Helper Tools so I am trying to delete that helper tool using the NSTask below is the code.

NSTask *task = [[NSTask alloc] init];
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Uninstall" ofType:@"sh"];
[task setLaunchPath:bundlePath];
[task setArguments:[NSArray arrayWithObjects:@"Uninstall.sh", nil]];
[task setStandardOutput:[NSPipe pipe]];
[task setStandardInput:[NSPipe pipe]];
[task launch];

It will prompt user password but then it says permission denied.

1

There are 1 answers

1
Malav Soni On BEST ANSWER

Below is the code I use to delete the helper tool from mac with admin privileges.

In fullScript write your terminal command and try.

-(void)removeHelperToolFromMac{

NSString * fullScript = @"write you terminal code here";

NSDictionary *errorInfo = [NSDictionary new];
NSString *script =  [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript];

NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script];
NSAppleEventDescriptor * eventResult = [appleScript executeAndReturnError:&errorInfo];

// Check errorInfo
if (!eventResult)
{
    // Describe common errors
    NSString *errorDescription = nil;
    if ([errorInfo valueForKey:NSAppleScriptErrorNumber])
    {
        NSNumber * errorNumber = (NSNumber *)[errorInfo valueForKey:NSAppleScriptErrorNumber];
        if ([errorNumber intValue] == -128)
            errorDescription = @"The administrator password is required to do this.";
    }
    // Set error message from provided message
    if (errorDescription == nil)
    {
        if ([errorInfo valueForKey:NSAppleScriptErrorMessage])
            errorDescription =  (NSString *)[errorInfo valueForKey:NSAppleScriptErrorMessage];
    }
}
else
{ 
    // Set output to the AppleScript's output
    // Successfully delete files
}
}

Hope this will help you.