osx程序自启动

本文介绍了Mac OS X中如何实现程序自启动,特别是通过登录项(login items)来实现。针对不同OS版本,提供了相应的API,如LaunchServices的LSSharedFileList.h。同时,提到了LaunchService在文档绑定和启动应用中的作用,并给出了相关文档链接和简单代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两个术语:

  • 启动项(startup item):在用户登录前就运行的item。
  • 登录项(login item):在用户登录后运行的item。每个用户都有一个不同的登录项集合。

这里介绍一下登录项。

一直以来,很多开发人员希望他们的程序能在用户登录时自动运行。但长久以来,并没有相关的API实现这一点,除了一些破解手法。要做到这一点,还依赖于OS的版本。

苹果的文档也详细介绍了多种不同的方法实现登录和登出。需要注意的是,launchd用户agent在10.4已经完全被抛弃了,应该避免使用。(http://developer.apple.com/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/CustomLogin.html)。

如果想知道一个登录项是否在运行被启动了,可以参考KnowingIfAppWasLaunchedFromLoginItems


LaunchService负责将文档绑定到引用程序,以及启动应用程序的系统级的功能。

它是ApplicationServices umbrella框架的一部分。

文档:

概念性的文档http://developer.apple.com/documentation/Carbon/Conceptual/LaunchServicesConcepts/index.html

参考引用文档http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/index.html

技术文档http://developer.apple.com/technotes/tn/tn2017.html



例子:

LaunchAtLoginController

一个很简单的例子,实现了一个用在Cocoa Mac Apps中来注册/反注册自身的Controller,使用了LSSharedFileList。

使用LSSharedFileList意味着你的永和可以选则System Preferences > Accounts > Login Items。

I'm currently using it on 10.6 (32/64) successfully. I've not investigated being able to flag the "Hide" flag which is possible from System Preferences.

1、实现(Code):

1.1 需要在登录时启动app?

LaunchAtLoginController *launchController = [[LaunchAtLoginController alloc] init];
BOOL launch = [launchController launchAtLogin];
[launchController release];

1.2 设置登录状态

LaunchAtLoginController *launchController = [[LaunchAtLoginController alloc] init];
[launchController setLaunchAtLogin:YES];
[launchController release];

2、实现(Interface Builder):

  • 打开Interface Builder
  • 在nib窗体中放置一个NSObject(蓝色的小盒子)
  • 从Inspector-Identity选项卡(Cmd+6)中设置Class到LaunchAtLoginController
  • 放置一个复选框在窗体/视图上
  • 从Inspector-Bindings选项卡(Cmd+4)找到Value item
    • 在Login Controller绑定到Launch
    • Model Key路径:launchAtLogin



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值