objective c - Login Helper App creating multiple OS X app instances -


i'm using servicemanagement framework add login item launch helper app, launch main app whenever user logs in. addloginitem , disableloginitem methods called if user selects or deselects "launch @ login" nsbutton of switch-type:

//add helper app login item - (void)addloginitem {     nslog(@"enable login item");     if (!smloginitemsetenabled((__bridge cfstringref)kloginhelperbundleidentifier, true)) {     } }  //disable helper app login item - (void)disableloginitem {     nslog(@"disable login item");     if (!smloginitemsetenabled((__bridge cfstringref)kloginhelperbundleidentifier, false)) {     } } 

the code helper app rather simple...

- (void)applicationdidfinishlaunching:(nsnotification *)notification {     [[nsworkspace sharedworkspace] launchapplication: @"my app"];      [[nsapplication sharedapplication] terminate:self]; } 

the problem when main app running, if user repeatedly selects , deflects 'launch @ login' button, second instance of main app launched. whereas, want happen main app launched upon user logging in.

upon looking @ smloginitem.h, saw documentation stated following:

 * @param enabled  * boolean enabled state of helper application. value effective  * logged in user. if true, helper application  * started (and upon subsequent logins) , kept running. if  * false, helper application no longer kept running. 

so, seems if helper app being launched every time addloginitem called. knowing that, modified helper app check if main app running. if is, terminate helper app. otherwise, we'll launch instance of main app, , terminate helper app.

- (void)applicationdidfinishlaunching:(nsnotification *)notification {     if ([nsrunningapplication runningapplicationswithbundleidentifier:@"com.mycompany.myapp"]){          [[nsapplication sharedapplication] terminate:self];      }      else {         [[nsworkspace sharedworkspace] launchapplication: @"my app"];          [[nsapplication sharedapplication] terminate:self];      } } 

however, if user selects , deselects "launch @ login" button repeatedly, second instance of main app created. have idea on how make sure second instance of main app not created helper app if user toggles "launch @ login" multiple times?

edit app not being distributed through mac app store, why not have code-signing or sandboxing enabled per instruction in this tutorial.

the problem being had few different copies of application on system (specifically, 1 on desktop , on in applications folder). removing 1 of these application instances resolved issue.


Comments