Mac OSX 开发,App开机自动启动

本文翻译自:

翻译来源

本项目demo地址:

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,如下图

Mac OSX 开发,App开机自动启动_第1张图片

2、在当前工程里创建一个辅助APP,直接添加一个target(苹果不允许当前APP直接注册到启动项,BUT提供了这种辅助APP的方法,辅助APP只用于启动当前APP),如下图

Mac OSX 开发,App开机自动启动_第2张图片

3、开启两个APP的沙箱功能,如下图

Mac OSX 开发,App开机自动启动_第3张图片

4、为辅助APP配置Build Setting,设置skip install 为yes,如下图

Mac OSX 开发,App开机自动启动_第4张图片

5、info.plist配置辅助APP Application is background only with the value: yes

6、配置主APP,在Build phase中添加一个copy file,Destination为 Wrapper,Subpath为Contents/Library/LoginItems,如下图

Mac OSX 开发,App开机自动启动_第5张图片

7、链接所需要的库

Mac OSX 开发,App开机自动启动_第6张图片

8、删除辅助APP的Window及ViewController

Mac OSX 开发,App开机自动启动_第7张图片

9、当前的配置已经全部结束,只需要添加几句简单的代码就可以了

(1)、主APP代码:

Mac OSX 开发,App开机自动启动_第8张图片

(2)、辅助APP代码:

Mac OSX 开发,App开机自动启动_第9张图片

现在已经完成了所有的功能,下面你只需要编译你的工程,导出你的APP,运行一次,关闭APP,然后你就可以重新启动试试效果了。


追加一个链接如果上传APPSTORE有问题

你可能感兴趣的:(Mac OSX 开发,App开机自动启动)