简体   繁体   中英

Remove Helper Tool using NSTask in MAC Application

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM