Reputation: 2485
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.
Upvotes: 11
Views: 10477
Reputation: 755
Apple is excellent at always putting new obstacles in the way of developers. Since Xcode 14, a new setting in the Build Settings is necessary for Repose's script to continue working.
Oleh Sherenhovskyi's script version no longer works because the Info.plist no longer contains any version information at all.
Upvotes: 0
Reputation: 2207
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"
Upvotes: 56
Reputation: 8396
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.
Upvotes: 5
Reputation: 81
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"
Upvotes: 8
Reputation: 3405
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
Upvotes: -3