在主APP Target中设置Build Setting 下Strip Debug Symbols During Copy为NO, 这个是默认的为No
分别开启主APP Target和Helper的App Sandbox
代码
主APP Target的自启动开关实现
-(void)daemon:(Boolean)install{
NSString *helperPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Library/LoginItems/iSimulatorHelper.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!");
}
// Setting login
// com.xxx.xxx为Helper的BundleID,ture/false设置开启还是关闭
if (!SMLoginItemSetEnabled((CFStringRef)@"org.skyfox.iSimulatorHelper",install))
{
NSLog(@"SMLoginItemSetEnabled failed!");
}
NSString *mainAPP = [NSBundle mainBundle].bundleIdentifier?:@"org.skyfox.iSimulator";
BOOL alreadRunning = NO;
NSArray *runnings = [NSWorkspace sharedWorkspace].runningApplications;
for (NSRunningApplication *app in runnings) {
if ([app.bundleIdentifier isEqualToString:mainAPP]) {
alreadRunning = YES;
break;
}
}
if (alreadRunning) {
[[NSDistributedNotificationCenter defaultCenter]postNotificationName:@"killme" object:[NSBundle mainBundle].bundleIdentifier];
}
}
Helper的AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString *mainAPP = @"org.skyfox.iSimulator";
// NSArray *runningArray = [NSRunningApplication runningApplicationsWithBundleIdentifier:mainAPP];
BOOL alreadRunning = NO;
NSArray *runnings = [NSWorkspace sharedWorkspace].runningApplications;
for (NSRunningApplication *app in runnings) {
if ([app.bundleIdentifier isEqualToString:mainAPP]) {
alreadRunning = YES;
break;
}
}
if (!alreadRunning) {
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(terminate) name:@"killme" object:mainAPP];
NSString *appPath = [[NSBundle mainBundle] bundlePath];
appPath = [appPath stringByReplacingOccurrencesOfString:@"/Contents/Library/LoginItems/iSimulatorHelper.app" withString:@""];
appPath = [appPath stringByAppendingPathComponent:@"Contents/MacOS/iSimulator"];
if (![[NSFileManager defaultManager] fileExistsAtPath:appPath])
{
return;
}
[[NSWorkspace sharedWorkspace] launchApplication:appPath];
}else{
[self terminate];
}
}
- (void)terminate{
[NSApp terminate:nil];
}
判断是不是开机自启动
- (BOOL)isStartAtLogin {
NSDictionary *dict = (__bridge NSDictionary*)SMJobCopyDictionary(kSMDomainUserLaunchd,
CFSTR("org.skyfox.iSimulatorHelper"));
BOOL contains = (dict!=NULL);
return contains;
}
最后编辑于:2021-12-05 15:19