iosswiftuiviewcontrollerlaunch-screen

Show version of app in launch screen with Swift


Scenario

I want to show the version of my iOS 9 app made with Swift.

What I did

I know how to get the version (let version: String = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)

I also have a custom label on my home screen.

My problem

My problem is now, that it is not allowed to use your own UIViewController for the splash / launch screen.

Many apps show their version right on that screen. That's why I think that there must be a way how to do it.


Solution

  • You can create a script in Build Phases of your project. But make sure you do a couple of things first.

    Go to your LaunchScreen.storyboard ViewController and create your version Label. Make sure to name your label to "APP_VERSION" in Identity Inspector pane -> Document -> Label.

    Then go to your project's build phases and create your script by clicking the "+" button in the top left corner, then select "New Run Script Phase" from the pulldown menu and then drag it before "Copy Bundle Resources" phase.

    UPDATE: My older answer didn't work in the newest Xcode environment. I've fixed the current issues and refactored the script. I'll update when I have the incrementer working. FYI, make sure you reinstall the app for the updated LaunchScreen.storyboard to show (due to system managing caching of the launch screen in latest versions of iOS).

    And here's the final working script with shell: /bin/sh in XCode 11 (Swift 5):

    #   ON/OFF Script Toggle (script ON with #, script OFF without #)
    #exit 0
    
    #   App vesion / Build version constants
    sourceFilePath="$PROJECT_DIR/$PROJECT_NAME/Base.lproj/LaunchScreen.storyboard"
    versionNumber="$MARKETING_VERSION"
    buildNumber="$CURRENT_PROJECT_VERSION"
    
    #   Output version & build numbers into a label on LaunchScreen.storyboard
    sed -i .bak -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"$versionNumber($buildNumber)\"/" "$sourceFilePath"