Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在cocoa应用程序中使用NSTask执行命令时执行sudo auth会话_Objective C_Cocoa_Sudo - Fatal编程技术网

Objective c 在cocoa应用程序中使用NSTask执行命令时执行sudo auth会话

Objective c 在cocoa应用程序中使用NSTask执行命令时执行sudo auth会话,objective-c,cocoa,sudo,Objective C,Cocoa,Sudo,我有一个需要用sudo执行命令的应用程序。如何请求密码,如果成功,则使用NSTask运行sudo命令。使用授权服务,Luke。(如果您曾经看到过“应用程序XYZ需要管理员密码才能继续”,这就是它的实现方式。它不会在工作表下使用sudo。) 如果您正在寻找更轻量级的解决方案,还有另一种方法。我编写了这个通用实现,它应该实现您想要的: - (BOOL) runProcessAsAdministrator:(NSString*)scriptPath withA

我有一个需要用sudo执行命令的应用程序。如何请求密码,如果成功,则使用NSTask运行sudo命令。

使用授权服务,Luke。(如果您曾经看到过“应用程序XYZ需要管理员密码才能继续”,这就是它的实现方式。它不会在工作表下使用sudo。)


如果您正在寻找更轻量级的解决方案,还有另一种方法。我编写了这个通用实现,它应该实现您想要的:

- (BOOL) runProcessAsAdministrator:(NSString*)scriptPath
                     withArguments:(NSArray *)arguments
                            output:(NSString **)output
                  errorDescription:(NSString **)errorDescription {

    NSString * allArgs = [arguments componentsJoinedByString:@" "];
    NSString * fullScript = [NSString stringWithFormat:@"%@ %@", scriptPath, allArgs];

    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
        *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];
        }

        return NO;
    }
    else
    {
        // Set output to the AppleScript's output
        *output = [eventResult stringValue];

        return YES;
    }
}
用法示例:

    NSString * output = nil;
    NSString * processErrorDescription = nil;
    BOOL success = [self runProcessAsAdministrator:@"/usr/bin/id"
                    withArguments:[NSArray arrayWithObjects:@"-un", nil]
                           output:&output
                            errorDescription:&processErrorDescription
                  asAdministrator:YES];


    if (!success) // Process failed to run
    {
         // ...look at errorDescription 
    }
    else
    {
         // ...process output
    }

帽子尖到

这个问题的可能重复不是那个问题的重复。这个问题是关于实际使用sudo的(尽管我同意正确的答案是“使用授权服务”)。