首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTask和AuthorizationCreate

NSTask和AuthorizationCreate
EN

Stack Overflow用户
提问于 2012-12-06 21:16:01
回答 1查看 1.5K关注 0票数 1

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

myStatus返回errAuthorizationSuccess。

以下是代码

代码语言:javascript
复制
//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,因为它是一个圆弧项目。我哪里做错了?寻求帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-08 03:20:57

我不知道我做错了什么,但我用非常奇怪的方法解决了它:(

代码语言:javascript
复制
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];

我不是很喜欢它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13744413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档