cocoa中得到root權限的幾種方法

目前我所知道的,在cocoa中得到root權限的方法有3種:shell

1. 經過AuthorizationCopyRights函數app

2. 在UI上添加一個鎖的樣子的控件,而後經過開關這個鎖來獲取root權限函數

3. 直接調用Applescript來以root權限執行腳本ui

 

其中方法1和2通常用來執行一個shell腳本或者一個可執行文件,方法3則直接執行一個applescript腳本。spa

固然方法1和2也能夠執行一個保存的applescript腳本,方法3也能夠用applescript腳原本執行shell或可執行文件。code

 

經過AuthorizationCopyRights

這種方法的缺陷是每次須要root權限都要執行一次,或者必須在程序開啓的時候獲取root權限。orm

請看下面的代碼:blog

    OSStatus status;
    AuthorizationRef authRef;
    status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authRef);
    
    AuthorizationRights authRights;
    AuthorizationItem authItems[1];
    
    authItems[0].name = kAuthorizationRightExecute;
    
    authRights.count = sizeof(authItems) / sizeof(authItems[0]);
    authRights.items = authItems;
    
    AuthorizationFlags authFlags = kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed;
    
    status = AuthorizationCopyRights(authRef, &authRights, kAuthorizationEmptyEnvironment, authFlags, NULL);
    if(status != errAuthorizationSuccess)
    {
        NSLog(@"Copy rights unsuccessful: %d",status);
    }
  .......... //中間省略 status
= AuthorizationExecuteWithPrivileges(authRef, 可執行文件的路徑, kAuthorizationFlagDefaults, 可執行文件須要的參數, nil); if (status != errAuthorizationSuccess) { NSLog(@"Error: %d", status); }

 

在UI上加一個鎖

這個方法比較靈活,打開鎖以後任何須要root權限的地方均可以直接用了,是我最推薦的一個方法ip

1. 首先給本身的項目添加兩個framework。Build phrase -> Link Binary With Libraries 添加Security.framework 和 SecurityInterface.framework.string

2. 在UI上拖拽進去一個Custom view控件,而後把它的Custom class設爲SFAuthorizationView

3. 在AppDelegate.h中添加代碼以下:

...........
#import <SecurityInterface/SFAuthorizationView.h>
#import <SystemConfiguration/SystemConfiguration.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
............
    IBOutlet SFAuthorizationView *authView;
............
}
-(BOOL)isUnlocked;

    添加兩個頭文件,並把custom view跟一個SFAuthorizationView 變量關聯起來。

4. 在AppDelegate.m中添加以下代碼:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    AuthorizationItem items = {kAuthorizationRightExecute,0,NULL,0};
    AuthorizationRights rights = {1,&items};
    [authView setAuthorizationRights:&rights];
    authView.delegate =self;
    [authView updateStatus:nil];
}

-(BOOL)isUnlocked
{
    return [authView authorizationState] == SFAuthorizationViewUnlockedState;
}

-(void)authorizationViewDidAuthorize:(SFAuthorizationView *)view
{

}

-(void)authorizationViewDidDeauthorize:(SFAuthorizationView *)view
{
}

-(void)doSomething
{
.............
    OSErr processErr = AuthorizationExecuteWithPrivileges([[authView authorization] authorizationRef], [binaryPath UTF8String], kAuthorizationFlagDefaults, (char* const*)args, nil);
    if (processErr != errAuthorizationSuccess)
    {
        NSLog(@"Error: %d", processErr);
    }
.............
}

applicationDidFinishLaunching 首先在程序啓動的時候初始化authView

isUnlocked  判斷那個鎖是否鎖住

authorizationViewDidAuthorize 和 authorizationViewDidDeauthorize 分別在鎖被打開和被鎖住的時候觸發,你能夠作你本身想作的事情,好比把按鈕變灰之類的

doSomething 就能夠利用authView authorization 來受權AuthorizationExecuteWithPrivileges進行root權限的操做了

   可參考http://bdunagan.com/2009/12/13/system-preferences-pane-lock/

 

利用applescript來執行root操做

   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)
    {
      // do something you want
    }
相關文章
相關標籤/搜索