为启动之前,过程中设置环境变量 [英] Set environment variable for the process before startup

查看:383
本文介绍了为启动之前,过程中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

我有一个应用程序的Mac OS捆绑使用了一些第三方的动态库和这些库依赖于某些环境变量,让我们将其命名为 ENV_VAR 。我想设置 ENV_VAR 到某一值时我的应用程序只,因为如果我将它设置为整个系统中,可能中断其他应用程序。它应该透明地给用户,即他只是通过双击运行该应用程序文件夹我的应用程序。我怎样才能实现呢?

请注意:动态库之前主要功能加载启动设置,因此在主这个变量并没有帮助


解决方案

您可以添加<一个href=\"https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html#//apple_ref/doc/uid/TP40009250-SW9\"相对=nofollow>键LSEnvironment您的应用程序包的Info.plist。该值可以是与键和值以及那些键 - 值对字符串将被添加到当您的应用程序是通过启动服务(例如从Finder或Dock而不是从终端)推出了环境的字典。

 &LT;密钥GT; LSEnvironment&LT; /键&GT;
&LT;&字典GT;
    &LT;密钥GT;&ENV_VAR LT; /键&GT;
    &LT;串GT; VALUE&LT; /串&GT;
&LT; /字典&GT;

不过,在我的测试(雪豹),这是一个有点片状测试,编辑现有应用程序的Info.plist中至少当。基本上,发射服务缓存应用程序的Info.plist的这一部分时,第一次遇到应用程序,不一定会承认在磁盘上的变化。有时你可以促使其重新读取由Info.plist中,例如,复制应用程序捆绑或暂时将其移动到其他文件夹。当然,矫枉过正的解决方案是使用 lsregister 刷新和重建缓存:

<$p$p><$c$c>/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -seed

这缓存的问题不会影响你的最终用户,只是你,你捏捏的Info.plist。此外,它不应该影响你,如果你做出改变你的源Info.plist中,然后用X code构建应用程序。

I have the following situation:

I have Mac OS bundle with application which uses some 3rd party dynamic libraries and those libraries depend on some environment variable, let's name it ENV_VAR. I want to set ENV_VAR to some value for my application only because if I set it for the whole system it may breaks some other apps. And it should work transparently to the user i.e. he just run my app from the Application folder by double clicking it. How can I achieve it?

NOTE: dynamic libraries are loaded before main functions starts hence setting this variable in the main doesn't help.

解决方案

You can add a key "LSEnvironment" to your app bundle's Info.plist. The value can be a dictionary with strings for keys and values and those key-value pairs will be added to the environment when your app is launched by Launch Services (e.g. from the Finder or Dock but not from the Terminal).

<key>LSEnvironment</key>
<dict>
    <key>ENV_VAR</key>
    <string>value</string>
</dict>

However, in my testing (on Snow Leopard), it was a bit flaky to test, at least when editing the Info.plist of an existing app. Basically, Launch Services caches this part of the app's Info.plist when it first encounters the app and won't necessarily recognize changes on disk. You can sometimes prompt it to reread the Info.plist by, for example, duplicating the app bundle or temporarily moving it to a different folder. Of course, the overkill solution would be to use lsregister to flush and rebuild the cache:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -seed

This caching issue won't affect your end users, just you as you tweak the Info.plist. Also, it shouldn't affect you if you make the change in your source Info.plist and then build the app using Xcode.

这篇关于为启动之前,过程中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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