如何在我的应用程序中从 Xcode 获取环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27269923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:17:58  来源:igfitidea点击:

How can I get an environment variable from Xcode in my app

iosxcodeswift

提问by MUECKE446

I have installed the Xcode plugin for XcodeColors from robbie hanson. (see https://github.com/robbiehanson/XcodeColors)

我已经为 robbie hanson 的 XcodeColors 安装了 Xcode 插件。(见https://github.com/robbiehanson/XcodeColors

If I test it in a playground

如果我在操场上测试

let dict = NSProcessInfo.processInfo().environment
let env = dict["XcodeColors"] as? String

envwould be "YES".

env将是“是”。

But, if I use the same code in my app, envwould be nil, because the app is running on their own process.

但是,如果我在我的应用程序中使用相同的代码,则env将为零,因为该应用程序在自己的进程上运行。

Because I would print out colored text with specific esc sequences only if the plugin is installed, I want get the information about the Xcode env var.

因为只有安装了插件,我才会用特定的 esc 序列打印出彩色文本,所以我想获取有关 Xcode env var 的信息。

How can I do that?

我怎样才能做到这一点?

回答by Imotep

Edit your scheme -> Select the "Run" section -> Select "Arguments" tab -> Add the environment variable.

编辑您的方案 -> 选择“运行”部分 -> 选择“参数”选项卡 -> 添加环境变量。

Be careful, environment variables are not set if you run the app without XCode.

请注意,如果您在没有 XCode 的情况下运行应用程序,则不会设置环境变量。

回答by Brian

I ran into the same problem with XcodeColors. I ended up solving it with a simple script build phase. It checks to see if XcodeColors is installed or not and sets/adds a key to the Info.plist in the build. So create a new "Run Script Build Phase" and put this in there:

我在使用 XcodeColors 时遇到了同样的问题。我最终用一个简单的脚本构建阶段解决了它。它检查是否安装了 XcodeColors 并在构建中为 Info.plist 设置/添加一个键。因此,创建一个新的“运行脚本构建阶段”并将其放在那里:

xcodeColorsDir="$USER_LIBRARY_DIR/Application Support/Developer/Shared/Xcode/Plugins/XcodeColors.xcplugin/"
xcodeColorsInstalled=0
if [ -d "$xcodeColorsDir" ]; then
    # Directory exists, therefore, XcodeColors is installed
    xcodeColorsInstalled=1
fi

infoPlistPath="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
existingValue=$(/usr/libexec/PlistBuddy -c "Print :XcodeColorsInstalled" "$infoPlistPath")
if [ -z "$existingValue" ]; then
    # Key already exists so overwrite it
    /usr/libexec/PlistBuddy -c "Add :XcodeColorsInstalled bool $xcodeColorsInstalled" "$infoPlistPath"
else
    # Key doesn't exist yet
    /usr/libexec/PlistBuddy -c "Set :XcodeColorsInstalled $xcodeColorsInstalled" "$infoPlistPath"
fi

Then, you can access the Info.plist param during runtime with something like:

然后,您可以在运行时使用以下内容访问 Info.plist 参数:

func isColorizedLoggingEnabled() -> Bool {
    if let colorizedLoggingEnabled = NSBundle.mainBundle().infoDictionary?["XcodeColorsInstalled"] as? Bool {
        return colorizedLoggingEnabled
    } else {
        return false
    }
}