MacOS开发——关于NSStatusBar开发小Tips

一、菜单栏设置开机自动启动
原文链接:https://blog.csdn.net/lovechris00/article/details/81483108

- (void)setLaunchAgents{
    
    NSString* launchFolder = [NSString stringWithFormat:@"%@/Library/LaunchAgents",NSHomeDirectory()];
    NSString * bundleID = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleIdentifierKey];
    
    NSString* dstLaunchPath = [launchFolder stringByAppendingFormat:@"/%@.plist",bundleID];
    
    NSLog(@"dstLaunchPath : %@",dstLaunchPath);
    //dstLaunchPath : /Users/administrator/Library/LaunchAgents/com.melissashu.www.MSLaunchAgentsLogin.plist
    
    NSFileManager* fm = [NSFileManager defaultManager];
    BOOL isDir = NO;
    //已经存在启动项中,就不必再创建
    if ([fm fileExistsAtPath:dstLaunchPath isDirectory:&isDir] && !isDir) {
        return;
    }
    //下面是一些配置
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
    NSMutableArray* arr = [[NSMutableArray alloc] init];
    [arr addObject:[[NSBundle mainBundle] executablePath]];
    [arr addObject:@"-runMode"];
    [arr addObject:@"autoLaunched"];
    [dict setObject:[NSNumber numberWithBool:true] forKey:@"RunAtLoad"];
    [dict setObject:bundleID forKey:@"Label"];
    [dict setObject:arr forKey:@"ProgramArguments"];
    isDir = NO;
    if (![fm fileExistsAtPath:launchFolder isDirectory:&isDir] && isDir) {
        [fm createDirectoryAtPath:launchFolder withIntermediateDirectories:NO attributes:nil error:nil];
    }
    
    NSLog(@"dict : %@",dict);
    
    [dict writeToFile:dstLaunchPath atomically:NO];
}

其他参考方式:
https://www.jianshu.com/p/19e95619c6a6

二、运行后隐藏Dock上的图标

设置info.plist 文件,添加Application is Agent 设置为yes

你可能感兴趣的:(MacOS开发——关于NSStatusBar开发小Tips)