How to read current app version in Xcode 11 with script

前端 未结 9 1742
野趣味
野趣味 2020-12-14 00:38

Until Xcode 11, I used a script that reads the current app version (for the AppStore) and help me change the LaunchScreen since we can\'t use swift for that

相关标签:
9条回答
  • 2020-12-14 01:08

    How about saving a value to CURRENT_PROJECT_VERSION ? did anyone managed to do this?

    I can get the value like

    buildNumber=$CURRENT_PROJECT_VERSION
    

    but this doesn't work:

    CURRENT_PROJECT_VERSION=""    or   $CURRENT_PROJECT_VERSION=""
    

    In my case I'm trying to set it to ""

    This line doesn't set the CURRENT_PROJECT_VERSION field too

    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$appInfoPlist"
    
    0 讨论(0)
  • 2020-12-14 01:09

    For Node.js: there is xcode package. Example of usage:

    const xcode = require('xcode');
    
    const project = xcode.project('ios/PROJECT_NAME.xcodeproj/project.pbxproj').parse(() => {
      const config = project.pbxXCBuildConfigurationSection();
      const releaseScheme = Object.keys(config).find(key => config[key].name === 'Release');
    
      const version = config[releaseScheme].buildSettings.MARKETING_VERSION;
    });
    

    Previously I used the plist package, but with latest xCode changes it became outdated, since I'm not able to extract a version from Info.plist for React Native projects.

    0 讨论(0)
  • 2020-12-14 01:21

    You can use it like any other project variable:

    sourceFilePath="$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"
    versionNumber="$MARKETING_VERSION"
    buildNumber="$CURRENT_PROJECT_VERSION"
    
    sed -i .bak -e "/userLabel=\"APP_VERSION_LABEL\"/s/text=\"[^\"]*\"/text=\"v$versionNumber\"/" "$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"
    
    0 讨论(0)
  • 2020-12-14 01:21

    Couldn't find right answer on the internet, so I started digging.

    Version and build numbers are displayed in ./PROJECTNAME.xcodeproj/project.pbxproj as MARKETING VERSION (MV) and CURRENT PROJECT VERSION (CPV).

    I used sed to get the numbers. It finds first occurrence of MV or CPV, removes everything except the number, and returns result. In order for this to work, you need to do 2 things:

    • navigate to projects root folder
    • change PROJECTNAME to your project's name

    Commands:

    version_number=`sed -n '/MARKETING_VERSION/{s/MARKETING_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./PROJECTNAME.xcodeproj/project.pbxproj`
    build_number=`sed -n '/CURRENT_PROJECT_VERSION/{s/CURRENT_PROJECT_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./PROJECTNAME.xcodeproj/project.pbxproj`
    

    Result:

    Note: If you have more targets in your workspace with different version and build numbers, this might or might not work for you, because it stops on first occurrence. In that case, good luck :)

    0 讨论(0)
  • 2020-12-14 01:22

    I miss here a solution for multiple targets and configurations:

    xcodebuild -target <target> -configuration <configuaration> -showBuildSettings  | grep -i 'MARKETING_VERSION' | sed 's/[ ]*MARKETING_VERSION = //'
    
    • target: the name of the target
    • configuration: Release, Debug
    0 讨论(0)
  • 2020-12-14 01:23

    I had similar issue and made it work by displaying MARKETING_VERSION itself:

    version="$MARKETING_VERSION"
    version+=" ("
    version+=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" $SRCROOT/MyApp/Info.plist`
    version+=")"
    
    /usr/libexec/PlistBuddy "$SRCROOT/MyApp/Settings.bundle/Root.plist" -c "set PreferenceSpecifiers:1:DefaultValue $version"
    
    0 讨论(0)
提交回复
热议问题