2011-05-24 107 views
1

我的應用程序需要身份驗證才能寫入hosts文件。我可以使用下面的一段代碼來做到這一點,我打電話。我的問題是,有時用戶需要在該程序實例中多次進行此更改 - 僅在第一次調用該密碼時纔會出現警告對話框,即使該函數稍後再次調用,密碼請求不顯示。任何人都可以對此有所瞭解嗎?謝謝。multiple AuthorizationExecuteWithPrivileges

- (void)someFunction { 
    AuthorizationRef authorizationRef; 
    OSStatus status; 
    status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, 
     kAuthorizationFlagDefaults, &authorizationRef); 

    //Run the tool using the authorization reference 
    char *tool = "/bin/mv"; 
    char *args[] = { "-f", "/tmp/hosts", "/etc/hosts" }; 
    FILE *pipe = NULL; 
    status = AuthorizationExecuteWithPrivileges(authorizationRef, 
      tool, kAuthorizationFlagDefaults, args, &pipe); 
} 
+0

對不起忘了提我與定義它早期的功能如下; \t AuthorizationRef authorizationRef; \t OSStatus狀態; \t status = AuthorizationCreate(NULL,kAuthorizationEmptyEnvironment,kAuthorizationFlagDefaults,&authorizationRef); – Phill 2011-05-24 17:27:44

回答

2

如果要強制重新認證,你應該叫

status = AuthorizationFree (authorizationRef, kAuthorizationFlagDestroyRights); 

AuthorizationExecuteWithPrivileges