인디노트

macOS 관리자 권한 얻어오기 본문

소스 팁/Objective C, Swift, iOS, macOS

macOS 관리자 권한 얻어오기

인디개발자 2023. 3. 29. 16:19

Objective-C를 사용하여 macOS에서 관리자 권한을 얻으려면 Authorization Services 프레임워크를 사용해야 합니다. 이를 통해 애플리케이션의 특정 작업을 수행하기 위해 관리자 권한이 필요한 경우 사용자에게 권한을 요청할 수 있습니다.

아래는 Objective-C를 사용하여 Authorization Services 프레임워크를 사용하여 관리자 권한을 요청하는 방법입니다.

 
#import <Foundation/Foundation.h>
#import <Security/Authorization.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        OSStatus status;
        AuthorizationRef authRef;
        
        status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authRef);
        if (status != errAuthorizationSuccess) {
            NSLog(@"Failed to create AuthorizationRef");
            return 1;
        }
        
        AuthorizationItem authItem = { kAuthorizationRightExecute, 0, NULL, 0 };
        AuthorizationRights authRights = { 1, &authItem };
        AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
        
        status = AuthorizationCopyRights(authRef, &authRights, kAuthorizationEmptyEnvironment, flags, NULL);
        if (status != errAuthorizationSuccess) {
            NSLog(@"Failed to obtain Authorization Rights");
            return 1;
        }
        
        NSLog(@"Authorization Granted");
        
        // 관리자 권한을 사용하여 수행할 작업을 여기에 추가하세요.
        
        AuthorizationFree(authRef, kAuthorizationFlagDestroyRights);
    }
    
    return 0;
}

 

#include <Security/Authorization.h>

- (void)requestAuthorization {
  OSStatus status;
  AuthorizationRef authRef;
  
  // Create an authorization reference
  status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authRef);
  if (status != errAuthorizationSuccess) {
    // Handle error
    return;
  }
  
  // Create a set of authorization rights
  AuthorizationItem authItem = { kAuthorizationRightExecute, 0, NULL, 0 };
  AuthorizationRights authRights = { 1, &authItem };
  
  // Request authorization from the user
  AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize;
  status = AuthorizationCopyRights(authRef, &authRights, kAuthorizationEmptyEnvironment, flags, NULL);
  if (status != errAuthorizationSuccess) {
    // Handle error
    return;
  }
  
  // At this point, the user has authorized the program to perform privileged operations
  
  // Perform privileged operations here...
  
  // Free the authorization reference when done
  AuthorizationFree(authRef, kAuthorizationFlagDefaults);
}

 

이 코드는 Authorization Services를 사용하여 사용자에게 프로그램이 실행되는 동안 승인을 요청합니다. AuthorizationCreate 함수를 사용하여 AuthorizationRef 객체를 만들고, AuthorizationItem 구조체를 사용하여 권한 집합을 만듭니다. 그런 다음 AuthorizationCopyRights 함수를 사용하여 권한을 요청합니다. 권한이 부여되면 프로그램이 권한이 필요한 작업을 수행할 수 있습니다. 마지막으로, AuthorizationFree 함수를 사용하여 AuthorizationRef 객체를 해제합니다.

이 코드에서 AuthorizationCopyRights 함수에서 사용되는 kAuthorizationFlagInteractionAllowed 플래그는 사용자가 대화형 방식으로 권한을 부여할 수 있는지 여부를 나타냅니다. 대화형 방식으로 권한을 요청하지 않으려면 kAuthorizationFlagDefaults 플래그만 사용하면 됩니다.

또한, 이 코드에서 사용된 AuthorizationRightExecute는 일반적인 권한 이름 중 하나입니다. 필요한 권한에 따라 다른 권한 이름을 사용해야 할 수도 있습니다. 자세한 내용은 macOS의 Authorization Services 문서를 참조하십시오.

반응형
Comments