How do I display the CFBundleShortVersionString in my LaunchStoryboard?

3 Solutions Collect From Internet About “How do I display the CFBundleShortVersionString in my LaunchStoryboard?”

I figured out the script to update the Version & Build label on LaunchScreen.storyboard based on the first answer without using any extra files. Unfortunately, Clemens Brockschmidt’s solution doesn’t work due to some Syntax errors and incorrect paths.

Make sure to name your label to “APP_VERSION” in Identity Inspector pane -> Document -> Label.

Also create your script before “Copy Bundle Resources” phase.

And here’s the final working script with shell: /bin/sh in XCode 9 (Swift 4):

#   ON/OFF Script Toggle (script ON with #, script OFF without #)
exit 0

#   Increment build number
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

#   Output version & build numbers into a label on LaunchScreen.storyboard
versionNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")
sed -i bak -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"Version: $versionNumber Build: $buildNumber\"/" $PROJECT_DIR/>>>CHANGE ME<<</Base.lproj/LaunchScreen.storyboard

Note: ‘>>>CHANGE ME<<<‘ is a project name.

As a BONUS I’ve included a build number incrementer and ON/OFF script toggle. Let me know if you have any issues or if this works for you.

As we all know, you can’t put code in a launch screen. And unfortunately there isn’t a built-in way to use a variable for a label’s text in the launch screen (similar to how you can preprocess Info.plist with values in a header file).

The only option available to achieve your goal would be to write your own script that updates the LaunchScreen.storyboard file and add that script as a custom Build Phase for your target.

To make this easier, I would setup your target to use a preprocessor file for Info.plist. Once that is done and working, you now have a separate and simple header file you can interrogate in your script to process the LaunchScreen.storyboard file.

Here’s a complete solution:

Create a file named Info.h and add it to the root of your project.

Add the following line:

#define APP_VERSION 2.6 // Update this version as needed

Now select your project’s target in Xcode and go to the General tab. Change the Version value from whatever number you have there to APP_VERSION.

Now select the Build Settings tab. Search on Info. Under the Packaging section, set the Preprocess Info.plist File to Yes. Also set the Info.plist preprocessing Prefix File to Info.h.

Now when you do a build, the CFBundleShortVersionString value in Info.plist will be set to the value in the Info.h file.

To get the label in the launch screen file updated to match, do the following:

Select your launch screen storyboard and then select the label that will contain the version number. Show the Identity Inspector pane. Enter APP_VERSION into the Label attribute. If you look at the storyboard file now, the XML for the label will now show a userLabel attribute with the value of APP_VERSION.

Go back to the project target and select the Build Phases tab. Click the + icon and choose to add a New Run Script Phase. Rename the new phase to something useful like “Update Launch Version”. Then drag the new phase to before the existing “Copy Bundle Resources” phase.

Now open the new “Update Launch Version” phase. Enter /bin/bash in the Shell field. Copy and paste the following code into the phase:

VERSION=`cat Info.h | grep APP_VERSION | cut -f3 -d' '`

sed -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"$VERSION\"/" Storyboard.storyboard > tmp.storyboard

Now do a clean build. This is a test at this point. Have a look at tmp.storyboard and make sure it looks correct and the label for the app version is showing the proper version.

Once that is working, update the above code to:

VERSION=`cat Info.h | grep APP_VERSION | cut -f3 -d' '`

sed -i bak -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"$VERSION\"/" Storyboard.storyboard

This final version actually updates the launch screen storyboard file. The previous version was a test to make sure everything else was working without risk to trashing your storyboard.

Edit

There is no way to make the launch screen dynamic. Doesn’t work good
with localizations too etc

A alternative is given below

Previous Answer

You should make your inital VC similar to the LaunchScreen.xib and in that make a label.

Now in the ViewController you can access the info plist through NSBundle method and set its value. This would make the transition from Launch screen to first VC smooth and look natural with version code animating in or something if you want

let appVersion = NSBundle.mainBundle().infoDictionary["CFBundleVersion"];
 myLabel.text = "\(appVersion)"