本文翻译自:
本项目demo地址:
苹果官方提供了两种方式: Service Management framework 和 shared file list
There are two ways to add a login item: using the Service Management framework, and using a shared file list
Login items installed using the Service Management framework are not visible in System Preferences and can only be removed by the application that installed them.
Login items installed using a shared file list are visible in System Preferences; users have direct control over them. If you use this API, your login item can be disabled by the user, so any other application that communicates with it it should have reasonable fallback behavior in case the login item is disabled.
这里提供 Service Management framework方式,该方式在系统的登录项中是不可见的。只有卸载App才能移除登录项
具体过程:
1、创建一个APP,如下图
2、在当前工程里创建一个辅助APP,直接添加一个target(苹果不允许当前APP直接注册到启动项,BUT提供了这种辅助APP的方法,辅助APP只用于启动当前APP),如下图
3、开启两个APP的沙箱功能,如下图
4、为辅助APP配置Build Setting,设置skip install 为yes,如下图
5、info.plist配置辅助APP Application is background only with the value: yes
6、配置主APP,在Build phase中添加一个copy file,Destination为 Wrapper,Subpath为Contents/Library/LoginItems,如下图
7、链接所需要的库
8、删除辅助APP的Window及ViewController
9、当前的配置已经全部结束,只需要添加几句简单的代码就可以了
(1)、主APP代码:
(2)、辅助APP代码:
现在已经完成了所有的功能,下面你只需要编译你的工程,导出你的APP,运行一次,关闭APP,然后你就可以重新启动试试效果了。
追加一个链接如果上传APPSTORE有问题
再次追加一个内容:自自动的APP 如果想彻底移除,需要把守护进程目录中的plist文件删除。
相关目录:
~/Library/launchAgents
/Library/launchAgents
/Library/launchDaemons