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

时间:2021-07-08 11:29:30

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

env would be "YES".

env将是“是的”。

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

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

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?

我怎么做呢?

2 个解决方案

#1


12  

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的应用程序,则不会设置环境变量。

#2


2  

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是否已安装,并设置/添加信息的键。在构建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:

然后,您可以访问信息。plist param在运行时具有如下内容:

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

#1


12  

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的应用程序,则不会设置环境变量。

#2


2  

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是否已安装,并设置/添加信息的键。在构建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:

然后,您可以访问信息。plist param在运行时具有如下内容:

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