Rendering version number into beta app icon using swift script

August 28, 2015

It’s quite handy to have multiple builds of same app on one device for developing purposes. I usually setup three different builds. I like to have AppStore version alongside βeta version that is distributed to testers, as well as having separate debug builds. Last is only used when I directly build and run from Xcode. I described my setup in blog post here. That blog post it’s a prerequisite for following technique of rendering app version and build number onto icon described below. All of the code is written in Swift 2.0 and sample project is Xcode 7.0 beta 6 project.

Start by downloading icon_processor.swift. You may want to download whole sample project (make sure you are viewing icon_version_rendering branch). It will make following along easier.

Copy icon_processor.swift to your project folder. Add new Run script build phase in iOS target just under target dependencies in Build Phases tab.01_build_phases

Define a path to icon_processor.swift, original icon, beta / debug icon. Define version and build number variables using PlistBuddy. Lastly define optional build type character.

PATH_TO_ORIGIN_ICON="$PROJECT_DIR/Pure Awesome/Assets.xcassets/AppIcon.appiconset"
PATH_TO_OUTPUT_ICON="$PROJECT_DIR/Pure Awesome/Assets.xcassets/AppIconDebug.appiconset"
VERSION_NUMBER=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")
BUILD_NUMBER=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")

Run script using swift -sdk $(xcrun –show-sdk-path –sdk macosx) (forcing it to point OS X SDK) and variables defined.

swift -sdk $(xcrun --show-sdk-path --sdk macosx) "$PATH_TO_ICON_PROCESSOR" "$PATH_TO_ORIGIN_ICON" "$PATH_TO_OUTPUT_ICON" "$VERSION_NUMBER" "$BUILD_NUMBER" "$BUILD_TYPE"

Script enumerates contents of PATH_TO_ORIGIN_ICON folder, uses my UIImageEffects category Swift, OS X port. I guess I should call it NSImageEffects. Renders iOS’s dock inspired blurred bar, BUILD_TYPE string, VERSION_NUMBER, BUILD_NUMBER onto every png image. It computes font size based on image size. Finds average icon colour from original icon image to use as text colour. And finally saves it to PATH_TO_OUTPUT_ICON. Since Pure Awesome sample app has multiple bundle identifiers and targets, I run it multiple times with different arguments.

02_iphoneAnd that’s all there is to it. Now we have debug and beta alongside AppStore build on same device, with version numbers rendered onto development builds icons. To wrap up, I have to say I love having power and Swift and Cocoa frameworks at my disposal when writing random scripts. Thank you for reading !

#Blog posts, #Coding, #iOS Development, #Swift