- 新建工程历恐,起名字為:mainProject
- 在新建的工程里旺上,創(chuàng)建target,起名字為:launchHelper
- 配置launchHelper
- 刪除launchHelper中的windows與Menu叹誉,讓它沒有可展示的Window。(注意不能將storyboard全部刪除闷旧,只刪除window及viewcontroller长豁,menu必須留著)
- 設(shè)置launchHelper的Info中Application is background only為YES
- 設(shè)置launchHelper中Build Setting下skip install為YES
- 配置mainProject
-
在主APP Target(mainProject)在build phases中,點擊左上角
將launchHelper打包進主target - 在主APP Target(mainProject)中添加CopyFile到Contents/Library/LoginItems
- 分別開啟mainProject和launchHelper的App Sandbox
- 添加啟動代碼
- 在mainProject中你要設(shè)置開機啟動的地方調(diào)用下面的代碼
-(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設(shè)置開啟還是關(guān)閉
if (!SMLoginItemSetEnabled((__bridge CFStringRef)helpApp,launch)) {
NSLog(@"SMLoginItemSetEnabled failed!");
}
}
- 在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];
}