Search code examples
iosxcodeimagemagickxcode9

Change AppIcon at build time in Xcode 9


I use a script to generate app icon at build time (just add "dev" title for debug mode). That's pretty easy using ImageMagick and Ghostscript (a lot of examples in google). Everything was ok before I started to use new Xcode 9. Now, I can see updated icons on MyAppName.app file but simulator/device shows AppIcon from assets. Any suggestions? Thx


Solution

  • It appears that when Xcode9 builds an app it now copies app icons from asset catalog into a private format and does not use app icon files from bundle directly. This might be related to new iOS11 feature where you can change app icons at runtime.

    There is a simple fix. What you have to do is change your app icons directly in asset catalog so must do 2 changes in your build setup:

    1. step - update run script phase

    Update your "generate icon" run script build phase so that updated icons are not copied to target bundle but instead icons are changed in asset catalog. Here is an example of my generate script, please notice target_path=$base_path line:

    function processIcon() {
        export PATH=$PATH:/usr/local/bin
        base_file=$1
        base_path=`find ${SRCROOT}/Cards/Assets.xcassets -name $base_file`
    
        if [[ ! -f ${base_path} || -z ${base_path} ]]; then
        echo "failed to find base_path for $base_file"
        return -1;
        fi
    
        target_file=`echo $base_file`
    
        #this is the change
        target_path=$base_path
    
        echo $target_path
    
        width=`identify -format %w ${base_path}`
    
        convert -background '#0008' -fill white -gravity center -size ${width}x40 caption:"${version} (${build}) ${commit}" ${base_path} +swap -gravity south -composite ${target_path}
    }
    

    2. step - change order of build phases

    you must change order of your build phases so that "Generate Icon" build phase is ordered before "Copy Bundle Resources" build phase: changed order of build phases

    Original idea and script code is from Krzysztof Zabłocki's blog post.