Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaunchScreen xib and displaying application version number from plist

I have created a LaunchScreen in Xcode and it appears with the default application title and copyright notice. However I would like to display the version number but I would rather pull the version number from the Info.plist rather than having to modify several places whenever the version number changes. Is this possible or do I need to give up on LaunchScreens and create a SplashScreenViewController to achieve this?

like image 326
rukiman Avatar asked Nov 05 '14 00:11

rukiman


2 Answers

Although it can now be a xib instead of bitmaps, the app launch screen still cannot execute any kind of application code. It's static, essentially. So it isn't possible to include a version number from a plist in your launch screen. Unless of course you manage to add some sort of build action to edit the launch screen (be it a xib or bitmaps).

like image 189
Clafou Avatar answered Oct 07 '22 13:10

Clafou


This is how I have done it with custom build rules. The basic idea is to add a placeholder string that will be replaced during the build process with proper version. The version is then reverted to the placeholder text.

  1. Create a script file $PROJECT_DIR/Scripts/splash-version.sh with contents:

IMPORTANT: Review XIB and PLIST paths match the ones used by your project

#!/bin/bash

xib_file="$PROJECT_DIR/Base.lproj/LaunchScreen.xib" # CHECK this path
plist_file="$PROJECT_DIR/$TARGET_NAME.plist".       # CHECK this path
bundle_ver=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$plist_file")
bundle_short_ver=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$plist_file")
str_ver="v$bundle_short_ver($bundle_ver)". # You can edit this. Beware this is used by sed as a regex 
placeholder_ver="#version#"                # You can edit this

if [ "$1" == "set" ]; then 
    if ! grep "$placeholder_ver" "$xib_file" >/dev/null; then
        echo "Version placeholder $placeholder_ver not found in XIB file $xib_file"
        exit 1
    fi
    sed -i -e "s/$placeholder_ver/$str_ver/" "$xib_file"
elif [ "$1" == "reset" ]; then
    sed -i -e "s/$str_ver/$placeholder_ver/" "$xib_file"
else
    echo "syntax: $0 set | reset"
    exit 1
fi
  1. Add execute permissions

  2. Add a new label in your XIB file with the text #version#. This is the placeholder string that will be replaced by the script with the proper version

  3. Add a "New Run Script Phase" and set the command "$PROJECT_DIR/Scripts/splash-version.sh" set

  4. Move it just before the "Copy Bundle Resources" phase

  5. Add a "New Run Script Phase" and set the command "$PROJECT_DIR/Scripts/splash-version.sh" reset

  6. Move it just after the "Copy Bundle Resources" phase. This steps reverts the version string back to the placeholder string.

like image 31
Paglian Avatar answered Oct 07 '22 14:10

Paglian