猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!

原文地址: http://blog.csdn.net/cocos2der/article/details/52104828

关于Mac下如何给自己App添加开机自启动功能,你可以了解下Mac Developer Library中的说明。

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.

可以看出,Apple推荐了两种方式:Service Management framework 和 shared file list。

这两种方式有差别:

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

  • 使用 shared file list 在系统的登录项中是可见的。用户可以直接在面板上控制他们。(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.) 原文还有一句大意是指这个API有隐患,所以在OS X 10.10系统上 API被大量Deprecated

下面我主要介绍的是使用Service Management framework 的方式添加开机自启动。

一、创建主工程

新建一个MainApp的osx工程App

二、添加自动启动Target

  • 我们需要注册一个Helper Target App用来作为开机自启动我们的MainApp,点击Targets下面的加号.

  • 添加一个新的OS X application。取名为MainAppHelper

三、设置配置属性

  • 删除MainAppHelper中的windows,让它没有可展示的Window。

  • 设置MainAppHelper的Info中Application is background only为YES

  • 设置MainAppHelper中Build Setting下skip install为YES

  • 在MainApp中添加CopyFile到Contents/Library/LoginItems

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

  • 分别开启MainApp和MainAppHelper的App Sandbox

四、添加启动代码

  • 请根据你自己的实际情况添加startupAppWhenLogin函数到你MainApp主工程中,程序运行后根据情况调用开启或者关闭自启动, 注意其中的BundleID改为你自己的,以及主App的名称。如果不确定名称是否是自己的工程名,可以直接右键自己的ipa,显示包内容,看下里面的文件夹你就明白了。
func startupAppWhenLogin(startup: Bool) {
        // 这里请填写你自己的Heler BundleID
        let launcherAppIdentifier = "com.cocos2dev.MainApp.MainAppHelper"

        // 开始注册/取消启动项
        SMLoginItemSetEnabled(launcherAppIdentifier, startup)

        var startedAtLogin = false
        for app in NSWorkspace.sharedWorkspace().runningApplications {
            if app.bundleIdentifier == launcherAppIdentifier {
                startedAtLogin = true
            }
        }

        if startedAtLogin {            NSDistributedNotificationCenter.defaultCenter().postNotificationName("killhelper", object: NSBundle.mainBundle().bundleIdentifier!)
        }
    }
  • 在MainAppHelper中,修改AppDelegate为如下代码:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application

        let mainAppIdentifier = "com.cocos2dev.MainApp"
        let running           = NSWorkspace.sharedWorkspace().runningApplications
        var alreadyRunning    = false

        for app in running {
            if app.bundleIdentifier == mainAppIdentifier {
                alreadyRunning = true
                break
            }
        }

        if !alreadyRunning {
            NSDistributedNotificationCenter.defaultCenter().addObserver(self, selector: "terminate", name: "killhelper", object: mainAppIdentifier)

            let path = NSBundle.mainBundle().bundlePath as NSString
            var components = path.pathComponents
            components.removeLast()
            components.removeLast()
            components.removeLast()
            components.append("MacOS")
            components.append("MainApp") //main app name

            let newPath = NSString.pathWithComponents(components)
            NSWorkspace.sharedWorkspace().launchApplication(newPath)
        } else {
            self.terminate()
        }
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

    func terminate() {
        //      NSLog("I'll be back!")
        NSApp.terminate(nil)
    }

}

以上就是一个自启动App的做法。当然如果你上架Mac Store,建议你不要默认就开启自启动,放到设置中,让用户自己选择开启/关闭。

2016-8-4 更新

  • 解决开启Sandbox后,请求网络数据出现"The connection to service named com.apple.nsurlstorage-cache was invalidated." 错误。勾选App Sandbox中Outgoing Connections(Client)

参考博客

Mac Developer Library

First OS X tutorial: How to launch an OS X app at login?

在SandBox沙盒下实现程序的开机启动

为你的MacOS App添加开机自启动(Swift)的更多相关文章

  1. linux添加开机自启动脚本示例详解

    linux下(以RedHat为范本)添加开机自启动脚本有两种方法,先来简单的; 一.在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,则:step1. 先修改好脚本,使其所有 ...

  2. win8系统添加开机自启动软件的方法(转)

    win8系统添加开机自启动软件的方法,把需要设置开机自启动的软件的快捷方式复制到下面任意路径就ok了.开机自启动路径如下:C:\ProgramData\Microsoft\Windows\Start ...

  3. Centos 下添加开机自启动服务和脚本

    最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...

  4. Centos 下添加开机自启动服务和脚本【转】

    最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...

  5. 在centos中添加开机自启动服务

    将服务的shell脚本添加到/etc/rc.d的rc.local文件的最后面,需要在服务名称的前面加上其路径. 例如我要将httpd添加到开机自启动中,需要在rc.local添加如下代码 /usr/s ...

  6. [Winform]setupfactory打包时添加开机自启动的脚本

    摘要 如果有这样的需求,需要软件开机自启动,该如何做呢?开机自启动的做法,就是修改注册表,将你的exe注册到注册表Run节点下. setupfactory 在安装的时候需要以管理员身份运行,这样可以保 ...

  7. chkconfig的原理 和添加开机自启动的办法

    当我们使用 chkconfig --list的时候 都会又  123456 这样的级别. 当某个级别是 on 他就会开机启动,当他是off 的时候他就不会开机自启动. 那么这是什么原因呢?他的 原理是 ...

  8. Centos7 下添加开机自启动服务和脚本

    1.添加开机自启服务 #设置jenkins服务为自启动服务 systemctl enable jenkins.service #启动jenkins服务 systemctl start jenkins. ...

  9. Centos7下shell脚本添加开机自启动

    添加开机自启脚本,注意都需要用绝对路径 psubscribe.sh脚本中的内容: nohup /usr/bin/php -f /data/aliyun51015cn/redisChannel/psub ...

随机推荐

  1. 51nod 1040 最大公约数之和(欧拉函数)

    1040 最大公约数之和 题目来源: rihkddd 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   给出一个n,求1-n这n个数,同n的最大公约数的和.比如: ...

  2. Python Web学习笔记之多道程序设计技术和操作系统的特性

    采用了多道程序设计技术的操作系统具有如下特性 : ① 并发性.它 是指两个或两个以上的事件或活动在同一时间间隔内发生.操作系统是一个并发系统,并发性是它的重要特征,操作系统的并发性指计算机系统中同时存 ...

  3. 3行代码快速实现Spring Boot Oauth2服务

    这里的3行代码并不是指真的只需要写3行代码,而是基于我已经写好的一个Spring Boot Oauth2服务.仅仅需要修改3行数据库配置信息,即可得到一个Spring Boot Oauth2服务. 项 ...

  4. 阿里Java研发工程师实习面经

    十分幸运 拿到阿里云的offer,感谢周围无数人对我的支持和鼓励,所以写篇面经希望可以帮助大家. 面试中,运气占很大一部分的,所以你们若是没有通过,一定不要气馁,继续加油. 每个努力的人 都值得钦佩, ...

  5. 39. Combination Sum(medium, backtrack 的经典应用, 重要)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  6. Promise--优雅的异步回调解决方案

    当一个接口需要依赖另一个接口的请求数据时,通常有两种解决方式,一个是将请求数据的接口设为同步,之后调另一个接口,另一个是在请求数据接口的成功回调里调另一个接口. 但是:当一个接口需要依赖很多个接口的请 ...

  7. iOS支付宝,微信,银联支付集成封装(上)

    一.集成支付宝支付 支付宝集成官方教程https://docs.open.alipay.com/204/105295/ 支付宝集成官方demo https://docs.open.alipay.com ...

  8. Vue2.0搭建脚手架流程

    介绍 Vue.js是一套构建用户界面的渐进式框架.Vue 只关注视图层,采用自底向上增量开发的设计.Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件. 安装node.js ...

  9. MongoDB 连接

    启动 MongoDB服务 在前面的教程中,我们已经讨论了如何启动MongoDB服务,你只需要在MongoDB安装目录的bin目录下执行'mongod'即可. 执行启动操作后,mongodb在输出一些必 ...

  10. 实验与作业(Python)-文件操作

    1.CSV文件的处理 下载-身份证号文件 导入: 读入"身份证号.txt",然后打印出来.注意:是否多打了一行,为什么? 读入"身份证号.txt",然后存储到& ...