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

前端 未结 1 2009
我在风中等你
我在风中等你 2021-01-02 10:36

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 sta

相关标签:
1条回答
  • 2021-01-02 11:16

    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.

    0 讨论(0)
提交回复
热议问题