Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify Info.plist to set "Application is agent(UIElement)" at runtime

Tags:

cocoa

Let's say I need to give the user the ability to choose by a preferences panel whether to use the app as "standard" (with dock icon and menu) or as an agent app (with status bar menu only).

I think I need to programmatically modify the app's "Info.plist" during execution, changing the parameter "Application is agent" to YES/NO.

Is this the right way?

P.S. You can find this behavior in "Sparrow".

like image 811
MatterGoal Avatar asked Aug 09 '11 15:08

MatterGoal


1 Answers

You should not modify your app's Info.plist file (or anything in your app's bundle) at runtime. This is bad practice and will also break your app if it is code signed. This is more important nowadays as all apps on the app store must be code signed.

A better option is to use the Application Services function TransformProcessType() to move your app from a background to a foreground app.

First, set the LSUIElement key in your app's Info.plist to YES and then check a user default at launch to determine if your app should be running as an agent or not:

#import <ApplicationServices/ApplicationServices.h>

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"LaunchAsAgentApp"]) 
    {
        ProcessSerialNumber psn = { 0, kCurrentProcess };
        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
        SetFrontProcess(&psn);
    }
}

@end

Make sure you don't forget to add the Application Services framework to your project.

like image 81
Rob Keniger Avatar answered Jan 22 '23 18:01

Rob Keniger