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

后端 未结 2 1242
不思量自难忘°
不思量自难忘° 2021-01-04 17:08

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

If I test it in a playground

         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-04 17:45

    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:

    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:

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

提交回复
热议问题