Objective-c MacOS running programs with Administrator privileges

Source: Internet
Author: User



Many operations under MacOS require administrator privileges, such as when we run chmod and can use sudo chmod at the command line to request to run with administrator privileges. However, programs written with Xcode cannot be used with sudo.



You need to write your own code to request permission. The following is an example of running chmod 777 as an administrator





bool ChmodFileWithElevatedPrivilegesFromLocation(NSString *location)
{
    // Create authorization reference
    OSStatus status;
    AuthorizationRef authorizationRef;

    status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
    if (status != errAuthorizationSuccess)
    {
        NSLog(@"Error Creating Initial Authorization: %d", status);
        return NO;
    }

    AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
    AuthorizationRights rights = {1, &right};
    AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed |
    kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
  
    status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
    if (status != errAuthorizationSuccess)
    {
        NSLog(@"Copy Rights Unsuccessful: %d", status);
        return NO;
    }
    
    // use chmod
    char *tool = "/bin/chmod";
    char *args[] = {"777", (char *)[location UTF8String], NULL};
    FILE *pipe = NULL;
    status = AuthorizationExecuteWithPrivileges(authorizationRef, tool, kAuthorizationFlagDefaults, args, &pipe);
    if (status != errAuthorizationSuccess)
    {
        NSLog(@"Error: %d", status);
        return NO;
    }
    
    status = AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
    return YES;
}

Calling methods







bool bRet = ChmodFileWithElevatedPrivilegesFromLocation("/Library");
if(bRet)
{
    NSLog(@"error");
}
else
{
    NSLog(@"sucess");
}








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Objective-c MacOS running programs with Administrator privileges


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.