Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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 NSTask和AuthorizationCreate_Objective C_Macos_Cocoa - Fatal编程技术网

Objective c NSTask和AuthorizationCreate

Objective c NSTask和AuthorizationCreate,objective-c,macos,cocoa,Objective C,Macos,Cocoa,在我的Cocoa应用程序中,我需要运行另一个具有root权限的应用程序,但它不起作用!NSTask没有所需的权限。 授权创建部分是我获得所需权限的地方。 运行NSTask是我运行另一个应用程序的部分 myStatus返回errAuthorizationSuccess 这是密码 //Authorization Create AuthorizationRef myAuthorizationRef; OSStatus myStatus; myStatus = AuthorizationCrea

在我的Cocoa应用程序中,我需要运行另一个具有root权限的应用程序,但它不起作用!NSTask没有所需的权限。 授权创建部分是我获得所需权限的地方。 运行NSTask是我运行另一个应用程序的部分

myStatus返回errAuthorizationSuccess

这是密码

//Authorization Create
    AuthorizationRef myAuthorizationRef;
OSStatus myStatus;
myStatus = AuthorizationCreate (NULL, kAuthorizationEmptyEnvironment,
                                kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed , &myAuthorizationRef);
AuthorizationItem myItems[1];  

myItems[0].name = "com.myOrganization.myProduct.myRight1";
myItems[0].valueLength = 0;
myItems[0].value = NULL;
myItems[0].flags = 0;

AuthorizationRights myRights;
myRights.count = sizeof (myItems) / sizeof (myItems[0]);
myRights.items = myItems;

AuthorizationFlags myFlags;
myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagExtendRights;

myStatus = AuthorizationCopyRights (myAuthorizationRef, &myRights,
                                    kAuthorizationEmptyEnvironment, myFlags, NULL);

if (myStatus==errAuthorizationSuccess)
{

//Running NSTask
    //setting license
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/opt/cprocsp/sbin/cpconfig"];

    NSArray *arguments;
    arguments = [NSArray arrayWithObjects:@"-license", @"-set",
             @"lalala", nil];
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    [task launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *string;
    string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    NSLog (@"grep returned:\n%@", string);
    [_logs insertText:string];
}
myStatus = AuthorizationFree (myAuthorizationRef,
                              kAuthorizationFlagDestroyRights);

我不能使用STPrivilege,因为它是一个ARC项目。我做错了什么?Thx寻求帮助。

我不知道我做错了什么,但通过非常奇怪的解决方案解决了它:

NSDictionary *error = [NSDictionary dictionary];
NSAppleScript *run = [[NSAppleScript alloc]initWithSource:@"do shell script \"/opt/cprocsp/sbin/cpconfig   -license -set lala\" with administrator privileges"];
[run executeAndReturnError:&error];

我不太喜欢它。

这个问题的答案有用吗?否:AuthorizationExecuteWithPrivileges已弃用,STPrivilegedTask无法使用,正如我所写:您的应用程序是否已沙盒?授权服务不工作,如果它是。不,它不是沙盒。启用权限复选框已关闭,应用程序沙盒也已关闭。我与您有相同的问题,但使用升级权限在shell上执行的命令不同。那么,您是否通过使用NSAppleScript而不是NSTask使其正常工作?