简体   繁体   中英

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.

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"

Updated @Repose script

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

#   Increment Build Number Bool (Increment ON with true, increment OFF with false)
shouldIncrement=true

#   App vesion / Build version constants
versionNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INFOPLIST_FILE")
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")

#   Increment build number
if [ "$shouldIncrement" = true ]; then
    buildNumber=$(($buildNumber + 1))
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"
fi

#   Output version & build numbers into a label on LaunchScreen.storyboard
sed -i bak -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"Version: $versionNumber Build: $buildNumber\"/" "$PROJECT_DIR/$PROJECT_NAME/Base.lproj/LaunchScreen.storyboard"

Its not allowed to use any UIViewController in LaunchScreen thus what other applications does is place a UILabel in LaunchScreen xib/Storyboard and write their version number as text.

There might be some work around's, Its still not preferred or allowed by Apple, don't waste your time searching you will get the following error if you tried to set custom class :

error: Illegal Configuration: Launch screens may not set custom classnames

NOTE: What usually apps do is create another LaunchScreen UIViewConttoller directly after the default launch screen with same design and presented without animation, for example to receive some data.

Making Your Version Number Super-Visible in iOS:

Start by adding a label to your LaunchScreen.storyboard :

  1. Format the label however you like. I like to use something subtle that you can read when you need to but that isn't very noticeable otherwise.
  2. Set Document/Label to APP_VERSION . You will use this value in the Build Phase script.

Continue by adding the custom Build Phase:

  1. Select your project in the upper-left-hand corner of Xcode.
  2. Select the app target towards the middle of the screen.

You should see a strip of tabs that includes General , Capabilities , […], Build Settings , and then Build Phases .

  1. Click on the + above the list of build phases and choose

    New Run Script Phase

  2. Rename the phase to something like “Show Version on LaunchScreen”. Xcode can be finicky about this part; if you can't rename the phase, don't worry too much about it.

  3. Move the phase somewhere above Copy Bundle Resources .

  4. Paste the following into the script area:

    # Output version & build number to the APP_VERSION label on LaunchScreen.storyboard
    versionNumber="$MARKETING_VERSION"
    buildNumber=$CURRENT_PROJECT_VERSION"
    
    sed -i "" -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"Version: $versionNumber ($buildNumber)\"/" "$PROJECT_DIR/$PROJECT_NAME/Base.lproj/LaunchScreen.storyboard"

source: https://thoughtbot.com/blog/making-your-version-number-super-visible

You can try to do it with LaunchScreen.storyboard . Try to add new LaunchScreen to your project with New File -> User Interface -> Launch Screen It automatically creates a storyboard type of LaunchScreen . Then you can change the initial Launch Screen on project settings -> General in this section:

在此处输入图片说明

And work with this storyboard with your custom class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM