cocoa获取管理员权限
来源:互联网 发布:网络正常魔域连接错误 编辑:程序博客网 时间:2024/06/05 00:10
+ (BOOL)removeFileWithElevatedPrivilegesFromLocation:(NSString *)location
{
// Create authorization reference
OSStatus status;
AuthorizationRef authorizationRef;
// AuthorizationCreate and pass NULL as the initial
// AuthorizationRights set so that the AuthorizationRef gets created
// successfully, and then later call AuthorizationCopyRights to
// determine or extend the allowable rights.
// CodeGo.net
status = AuthorizationCreate(NULL,kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
if (status !=errAuthorizationSuccess)
{
NSLog(@"Error Creating Initial Authorization: %d", status);
returnNO;
}
// kAuthorizationRightExecute == "system.privilege.admin"
AuthorizationItem right = {kAuthorizationRightExecute,0, NULL,0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags =kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |kAuthorizationFlagExtendRights;
// Call AuthorizationCopyRights to determine or extend the allowable rights.
status = AuthorizationCopyRights(authorizationRef, &rights,NULL, flags, NULL);
if (status !=errAuthorizationSuccess)
{
NSLog(@"Copy Rights Unsuccessful: %d", status);
returnNO;
}
// use rm tool with -rf
char *tool ="/bin/rm";
char *args[] = {"-rf", (char *)[locationUTF8String], NULL};
FILE *pipe =NULL;
status = AuthorizationExecuteWithPrivileges(authorizationRef, tool,kAuthorizationFlagDefaults, args, &pipe);
if (status !=errAuthorizationSuccess)
{
NSLog(@"Error: %d", status);
returnNO;
}
// The only way to guarantee that a credential acquired when you
// request a right is not shared with other authorization instances is
// to destroy the credential. To do so, call the AuthorizationFree
// function with the flag kAuthorizationFlagDestroyRights.
// CodeGo.net
status = AuthorizationFree(authorizationRef,kAuthorizationFlagDestroyRights);
returnYES;
}
- cocoa获取管理员权限
- Windows7管理员权限获取
- win7获取管理员权限
- 如何获取win7管理员权限
- ShellExecuteEX编程 --- 获取管理员权限
- 获取win7 管理员权限
- Window7获取/删除管理员权限
- Win7获取管理员权限
- win7获取管理员权限
- 通过 manifest 获取管理员权限
- linux应用程序获取管理员权限
- linux普通用户获取管理员权限
- win10 获取超级管理员权限
- Win7 获取管理员权限
- VC程序获取管理员权限
- linux普通用户获取管理员权限
- C#获取管理员权限
- bat文件获取管理员权限
- Android拍照及裁剪实现
- 推荐一本ArmLinux系统底层的书
- 绝对路径和相对路径
- java自动装箱拆箱
- 博客第一篇,立贴为证!
- cocoa获取管理员权限
- Dynamics CRM 2015/2016 Web API:Unbound Custom Action 和 Bound Custom Action
- Spring使用JdbcTemplate实现对数据库操作
- Scala学习1之用sbt和脚本一步编译打包运行scala程序
- iOS判断设备
- 【CSS】初识选择器(1)
- 数据结构基础
- [leetcode]Valid Parentheses
- 【开源中国Android客户端】源码分析(二)网络数据传输接口