Mac开发-app开机启动

  1. 新建工程,起名字为:mainProject
  2. 在新建的工程里,创建target,起名字为:launchHelper
  3. 配置launchHelper
    1. 删除launchHelper中的windows与Menu,让它没有可展示的Window。(注意不能将storyboard全部删除,只删除window及viewcontroller,menu必须留着)
  4. 设置launchHelper的Info中Application is background only为YES
  5. 设置launchHelper中Build Setting下skip install为YES
  6. 配置mainProject
  7. 在主APP Target(mainProject)在build phases中,点击左上角


    将launchHelper打包进主target
  8. 在主APP Target(mainProject)中添加CopyFile到Contents/Library/LoginItems
  9. 分别开启mainProject和launchHelper的App Sandbox
  10. 添加启动代码
  11. 在mainProject中你要设置开机启动的地方调用下面的代码
-(void)autoLaunch:(BOOL)launch{
    NSString *helpApp = @"com.kingsoft.LaunchHelper";
    NSString *helperPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Library/LoginItems/LaunchHelper.app"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:helperPath]) {
        return;
    }
    NSURL *helperUrl = [NSURL fileURLWithPath:helperPath];
    // Registering helper app
    if (LSRegisterURL((__bridge CFURLRef)helperUrl, true) != noErr) {
        NSLog(@"LSRegisterURL failed!");
    }
    // com.xxx.xxx为Helper的BundleID,ture/false设置开启还是关闭
    if (!SMLoginItemSetEnabled((__bridge CFStringRef)helpApp,launch)) {
        NSLog(@"SMLoginItemSetEnabled failed!");
    }
}
  1. 在launchHelper中Appdelegate中
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSString *mainAPP = @"com.kingsoft.powerWordForMac";
    NSString *appPath = [[NSBundle mainBundle] bundlePath];
    appPath = [appPath stringByReplacingOccurrencesOfString:@"/Contents/Library/LoginItems/LaunchHelper.app" withString:@""];
    //appPath = [appPath stringByAppendingPathComponent:@"Contents/MacOS/PowerWordForMac"];
    appPath = [appPath stringByAppendingPathComponent:@"Contents/MacOS/金山词霸"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:appPath]) {
        return;
    }
    NSArray *runningArray = [NSRunningApplication runningApplicationsWithBundleIdentifier:mainAPP];
    if ([runningArray count] > 0) {
        return;
    }
    [[NSWorkspace sharedWorkspace] launchApplication:appPath];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,603评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,338评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,541评论 4 61
  • 不计风险的投资都是耍流氓,想想吧,耍了自己多少年了,做什么事都是头脑一热,三分钟热度,计算过损失的成本吗,别以为只...
    觞咏阅读 846评论 0 0
  • 醒来 醒来 醒来 无论你过去在哪里 来自什么星球 拖延星球 焦虑星球 游戏星球 混沌星球 愚蠢星球 懒惰星球 这一...
    诗意人阅读 379评论 0 0

友情链接更多精彩内容