I have an NSStatusItem app that I’ve had to migrate to a sandboxed app for 
release in the MAS. In its preferences window, there is a checkbox for “Launch 
at login”. If I launch the app with no preferences set (i.e. defaults delete 
my.bundle.identifier, rm ~/Library/Container/my.bundleIdentifier), open the 
preferences and check the login checkbox. the app behaves as expected in that 
the checkbox gets checked and if I logout/login, the app is launched. If I open 
the app’s preferences now, the checkbox is checked as it should be, but if I 
uncheck it and then check it again, I get a second instance of my app in the 
menu bar. Clearing the preferences again, starts the cycle over. The really odd 
thing is that I’ve only noticed this on a brand new MB Pro 15”. I also have an 
iMac that is ~2010 and another MB Pro ~2008, neither of which exhibit this 
behavior. All machines are running Yosemite. I’ve yet to test this on older 
OS’s. I never get more than two instances no matter how many times I 
uncheck/check the checkbox.  Now there are actually two separate processes, Top 
shows two different PID’s.  I thought Launch Services was supposed to prevent 
this. Since this is a sandboxed app, I added a helper app to the project which 
launches the app depending on if the checkbox was checked. I’ve added code to 
that helper to check if the app is already running. (look through 
NSRunningApplications and check for my bundle identifier). This doesn’t prevent 
the second instance from occurring. So anyone have any ideas why this might be 
happening? 

On a side note. how does one debug the helper app within the context of the 
main application? I can launch the helper app through its subproject and step 
through it with the debugger. If I have the main app already running when I do 
this, the helper quits without attempting to launch another instance of the 
main app. But I don’t even get NSLogs from the helper when its running from the 
main app.

Here is the code in the window controller for the preferences window:

(IBAction)toggleLoginStatus:(NSButton*)sender{
        
        
        if(!SMLoginItemSetEnabled((__bridge 
CFStringRef)@“my.bundle.Identifier", (BOOL)[sender state])){
                NSLog(@“Crap!");
        }
}

Here is the code for the helper app:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        
        NSString * path;
        
        BOOL alreadyRunning = NO;
       
        NSArray *running = [[NSWorkspace sharedWorkspace] runningApplications];
        
        for (NSRunningApplication *app in running) {
                if ([[app bundleIdentifier] 
isEqualToString:@“my.bundle.Identifier"]) {
                        alreadyRunning = YES;
                }
        }
        
        if(!alreadyRunning){
                path = [[[[[[NSBundle mainBundle] bundlePath] 
stringByDeletingLastPathComponent]
                                                
stringByDeletingLastPathComponent]
                                                        
stringByDeletingLastPathComponent]
                                                                
stringByDeletingLastPathComponent];
                [[NSWorkspace sharedWorkspace] launchApplication:path];
        }
        
        [NSApp terminate:nil];
}

- 
(And a slight rant:
        The LSSharedFileList method worked just perfectly to create a startup 
item. Such a shame I had to dump perfectly good working code because of 
sandboxing.)

Thanks



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to