修改Info.plist以设置“Application is agent(UIElement)”在运行时 [英] Modify Info.plist to set "Application is agent(UIElement)" at runtime

查看:2061
本文介绍了修改Info.plist以设置“Application is agent(UIElement)”在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我需要让用户能够通过首选项面板选择是将应用程序用作标准(使用停靠栏图标和菜单)还是作为代理应用程序(仅限状态栏菜单)。

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).

我想我需要在执行期间以编程方式修改应用程序的Info.plist,将参数Application is agent更改为YES / NO。

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

这是正确的方法吗?

您可以在Sparrow中找到此行为。

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

推荐答案

您不应该修改应用程序的 Info.plist 文件(或应用程序包中的任何内容)。这是坏的做法,如果它是代码签名也将打破你的应用程序。现在更重要的是,应用商店上的所有应用都必须进行代码签名。

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.

更好的选择是使用应用服务功能 TransformProcessType )将您的应用从后台移动到前台应用。

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

首先,设置 LSUIElement 键入您的应用程序的 Info.plist YES ,然后在启动时检查用户默认值,应作为代理运行:

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.

这篇关于修改Info.plist以设置“Application is agent(UIElement)”在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆