Mac开发-app开机启动

作者: Ryan___ | 来源:发表于2017-07-07 22:09 被阅读139次
  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];
}

相关文章

  • Mac开发-app开机启动

    新建工程,起名字为:mainProject 在新建的工程里,创建target,起名字为:launchHelper ...

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

    本文翻译自: 翻译来源 本项目demo地址: Demo 地址 苹果官方提供了两种方式: Service Manag...

  • MAC启动项

    Mac设计开机启动项 MAC设置开机启动项分两种级别。简单和深度。 简单设置开机启动项 此种方法是通过系统->用户...

  • mongodb

    mac 安装 开机启动mongodb命令 (配置开机自启) 使用launchctl启动mongodb server...

  • Mac设置开机启动

    Mac设置开机启动 本文针对命令行使用的开发工具或自己编写的脚本设置开机启动,图形化界面的程序仅需要设置->用户与...

  • Android开发:APP自启动的实现

    Android的自启动类似于Windows的开机启动,允许开发者让自己的APP在开机的时候做一些操作,如启动一个后...

  • 系统开机启动管理:Startupizer2 for Mac

    想要快速去管理你Mac电脑的开机启动项么?startupizer Mac一款Mac OS平台上的开机启动项管理工具...

  • android多维度分析性能优化

    1、app启动 app启动流程: 开机------>BootLoader(引导芯片)------>Linux Ke...

  • App开机自启动

    App开机自启动 通过开机广播来实现自启动 定义开机广播 在AndroidManifest.xml注册BootCo...

  • MacOS开发笔记8-定制开机启动

    最近的,开发中遇到一个问题,就是新开发的程序需要开机启动,并且需要随时可以关闭这个开机启动功能。开机启动的网上有很...

网友评论

    本文标题:Mac开发-app开机启动

    本文链接:https://www.haomeiwen.com/subject/vodaqxtx.html