ServiceManagement
零、ServiceManagement.framework 简述
用来加载或者卸载守护进程服务。在应用内读取或调整 launchd 字典。
简单看来,这个框架很小
ServiceManagement.framework 包含
ServiceManagement.h
SMErrors.h
SMLoginItem.h
一、ServiceManagement.h
是 ServiceManagement 框架主要的头文件;这个头文件提供加载或不加载守护进程应用,并从应用中读取和操纵 job 字典。
1、常量
1.1 kSMRightBlessPrivilegedHelper
/*!
* @abstract
* The authorization rights key for blessing and installing a privileged helper
* tool.
* 用来安装高权限helper工具 的的授权key
*/
#define kSMRightBlessPrivilegedHelper "com.apple.ServiceManagement.blesshelper"
1.2 kSMRightModifySystemDaemons
/*!
* @abstract
* The authorization rights key for modifying system daemons.
* 用来调整系统守护进行的授权key
*/
#define kSMRightModifySystemDaemons "com.apple.ServiceManagement.daemons.modify"
2、常量
const CFStringRef kSMDomainSystemLaunchd;
展示 Mach bootstrap 特权上下文的常量。使用这个上下文,需要root 权限。
const CFStringRef kSMDomainUserLaunchd;
展示和调用者UID关联的 Mach bootstrap 上线文。在iOS中,这个词是 kSMDomainSystemLaunchd 的同义词。
3、方法
3.1 SMJobBless
Boolean SMJobBless(CFStringRef domain, CFStringRef executableLabel, AuthorizationRef auth, CFErrorRef *outError);
- domain 只支持 kSMDomainSystemLaunchd;
- 需要特权安装的。executableLabel 必须是应用程序 Info.plist 中 SMPrivilegedExecutables 中某个键对应的值;
- auth: 包含 kSMRightBlessPrivilegedHelper 权限的参数。
- outError:提交执行工具时遇到错误的输出参数,提交成功的话,返回NULL。这个参数也可以为NULL。
官方地址:https://developer.apple.com/documentation/servicemanagement/1431078-smjobbless?language=objc
3.2 其他过期方法
__OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6, __MAC_10_10, __IPHONE_3_0, __IPHONE_8_0)
在mac os x 10.6 开始引进这个方法,但是在10.10之后不再建议使用; iOS 上 3.0 开始引进这个方法,但是在 8.0之后废弃了。
CFDictionaryRef SMJobCopyDictionary(CFStringRef domain, CFStringRef jobLabel);
CFArrayRef SMCopyAllJobDictionaries(CFStringRef domain);
Boolean SMJobSubmit(CFStringRef domain, CFDictionaryRef job, AuthorizationRef auth, CFErrorRef *outError);
Boolean SMJobRemove(CFStringRef domain, CFStringRef jobLabel,
AuthorizationRef auth, Boolean wait, CFErrorRef *outError);
二、SMErrors.h
包含 ServiceManagement 框架返回的错误码;使用时,不需要特定引入这个框架;ServiceManagement.h 已经引入。
1、常量
__OSX_AVAILABLE_BUT_DEPRECATED
可以用但是过期的
kSMErrorDomainIPC
kSMErrorDomainFramework
kSMErrorDomainLaunchd
2、 kSMError 枚举
enum {
kSMErrorInternalFailure = 2,
kSMErrorInvalidSignature,
kSMErrorAuthorizationFailure,
kSMErrorToolNotValid,
kSMErrorJobNotFound,
kSMErrorServiceUnavailable,
kSMErrorJobPlistNotFound,
kSMErrorJobMustBeEnabled,
kSMErrorInvalidPlist,
};
三、SMLoginItem.h
包含的头文件:<sys/cdefs.h>
1、SMLoginItemSetEnabled 方法
只有一个方法:
Boolean
SMLoginItemSetEnabled(CFStringRef identifier, Boolean enabled);
Mac 10.6后可用,iPhone 不可用。
允许helper 程序放在主程序包中的 Contents/Library/LoginItems
目录;
其中:
参数 identifier 是这个helper 程序的 identifier。
参数 enabled 标记这个helper 程序的状态;这个值只有在当前用户登录后才有效。如果为true,helper 程序会马上启动并一直运行;设置为 false,就不会再运行。
结果返回 true,代表设置已经生效。