Objective-C使用AuthorizationCreate以root身份运行代码

时间:2015-12-09 21:34:12

标签: objective-c macos cocoa

我正在尝试在我的Cocoa-App中以root身份运行几行代码。 我试图通过AuthorizationCreate获取root权限并在运行代码后释放这些权限。

问题是,我在//运行我的代码部分没有root权限,也不知道原因。

希望有人能弄明白为什么。 谢谢!

   AuthorizationRef myAuthorizationRef;
OSStatus myStatus;

myStatus = AuthorizationCreate (NULL, kAuthorizationEmptyEnvironment,
                                kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed , &myAuthorizationRef);
AuthorizationItem myItems[1];

myItems[0].name = kAuthorizationRightExecute;
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)
{

  //Run my Code


}

myStatus = AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDestroyRights);

0 个答案:

没有答案