Mac OS X 获取Root权限代码

时间:2021-06-01 14:53:41
	OSStatus myStatus;
	AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
	AuthorizationRef myAuthorizationRef;
	myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
	
	if (myStatus != errAuthorizationSuccess) 
	{
		return myStatus;
	}
	
	AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
	AuthorizationRights myRights = {1, &myItems};
	myFlags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
	myStatus = AuthorizationCopyRights (myAuthorizationRef, &myRights, NULL, myFlags, NULL );
	
	if (myStatus == errAuthorizationSuccess)
	{
		[self delFileFrom:IREDA_PATH Use:myAuthorizationRef];
	}
	
	AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDefaults);
	
	NSLog(@"卸载完成");