Clemens Brockschmidt Clemens Brockschmidt - 12 days ago 5
iOS Question

How do I display the CFBundleShortVersionString in my LaunchStoryboard?

Is there any way to display the CFBundleShortVersionString as a UILabel text in my LaunchStoryboard without entering it by hand every time it increments? I know how to do it in code, but it is not possible to run code while the LaunchStoryboard is shown.

Is it possible through Xcode variables?

Answer

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.

Comments