最新消息:iOS编程开发交流群(6906921) ,Mac.Cocoa开发交流群(7758675) 欢迎iOS/macOS开发编程爱好及学习者加入!

Cocoa开发之APP开机自启动

Cocoa 天狐 13834浏览 1评论

macOS app 开机自启动虽然有很多种方法,但是在需要上架APPStore的情况下,访问沙盒外的文件会被拒绝.

苹果官方提供了两种方式: 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

Service Management framework 在系统的登录项中是不可见的。只有卸载App才能移除登录项

1.这里认为你已经有了一个将要被启动的主工程与主Target

我的app名为iSimulator, ServiceManagement.framework

2.在主工程添加自动启动Target

命名最好是主Target名+Helper    比如iSimulatorHelper

3.配置XXXHelper

 

  • 删除XXXHelper中的windows与Menu,让它没有可展示的Window。
  • 设置XXXHelper的Info中Application is background only为YES
  • 设置XXXHelper中Build Setting下skip install为YES

4.在主APP Target中添加CopyFile到Contents/Library/LoginItems

随后点击+ 把helper app添加进来.

在主APP Target中设置Build Setting 下Strip Debug Symbols During Copy为NO, 这个是默认的为No

分别开启主APP Target和Helper的App Sandbox

代码

主APP Target的自启动开关实践中

Helper的AppDelegate

判断是不是开机自启动

对应的终端查看命令

其他问题

当关闭开机启动的时候,发现console.app中还会显示这些log

手动编辑配置文件删除:

重启, 运行

确认列表中没有你的helper

 

注意:你的主程序必须在Application的目录下,开机启动的设置才会生效,否则会失败。并且主app要用开发者证书打包。

 

https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLoginItems.html

https://developer.apple.com/library/content/documentation/Security/Conceptual/AppSandboxDesignGuide/DesigningYourSandbox/DesigningYourSandbox.html

转载请注明:天狐博客 » Cocoa开发之APP开机自启动

微信 OR 支付宝 扫描二维码
为天狐 打赏
非常感谢你的支持,哥会继续努力!
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 大神,为什么我helper里面的applicationDidFinishLaunching函数没有启动 🙄
    lslboy7年前 (2017-11-30)回复