2012-09-24 31 views
1

我試圖用SMJobSubmit從普通的桌面應用程序中啓動一個我在Xcode中創建的Unix可執行文件。 (GUI部分應該稱爲命令行幫助工具。)啓動守護程序可以運行哪種文件?

下面是我用來嘗試提交作業的代碼。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    [authView setString:kSMRightModifySystemDaemons]; 
    [authView setAutoupdate:YES]; 
    [authView setDelegate:self]; 
} 

- (IBAction)pressedButton:(id)sender 
{ 
    /* 
     authView is an instance of SFAuthorizationView. At this point, the user 
     already clicked the padlock and entered his password. 
    */ 
    authRef = [[authView authorization] authorizationRef]; 

    //toolPath points to the file in the app's Resource folder. 
    NSArray* call = [NSArray arrayWithObject:toolPath]; 

    NSDictionary* jobSpec = [NSDictionary dictionaryWithObjectsAndKeys: 
          call, @"ProgramArguments", 
          jobLabel, @"Label", 
          [NSNumber numberWithBool:YES], @"RunAtLoad", 
          nil]; 

    CFErrorRef submitError = nil; 
    BOOL submitResult = SMJobSubmit(kSMDomainSystemLaunchd, 
            (__bridge CFDictionaryRef)(jobSpec), 
            authRef, 
            &submitError); 

    if(!submitResult) 
    { 
     NSLog(@"Job submit failed. %@", submitError); 
     return; 
    } 
} 

submitResult總是原來是true,但我的計劃似乎永遠不會執行。我已經通過終端手動執行了我的程序,它完美地工作。幫助器工具應該將文件寫入文件系統上的某個位置,並且不帶任何參數。但是,當我發送它來啓動守護進程時,沒有任何反應。它被標記爲可執行文件。我也驗證了路徑。這是正確的。

那麼,我還想知道啓動守護進程是否只能運行某些類型的文件?除非我在代碼中搞砸了一些東西。

回答

0

您創建的授權引用應該有權利kSMRightModifySystemDaemons能夠使用launchd運行可執行文件。

使用前

authRef = [[authView authorization] authorizationRef];

東西

[authview SetAuthorizationRights:kSMRightModifySystemDaemons]

聲明應該工作。對於kSMRightModifySystemDaemons的定義,您可能需要包含<ServiceManagement/ServiceManagement.h>

+0

我已經發布了更多我的代碼。我有'[authView setString:kSMRightModifySystemDaemons];'在我的'applicationDidFinishLaunching'方法中。該API說:「必須在視圖正確顯示之前調用此方法或'setAuthorizationRights:'方法。」 – 425nesp

相關問題