最近讀到一篇文章(http://www.merowing.info/2013/03/overlaying-application-version-on-top-of-your-icon/)介紹了一種非常簡單的,把版本信息顯示到iOS app的icon上的方式, 有了這個技能,在測試多版本的時候,測試人員可以直接從icon上看到當前測試的版本,無需在到HockeyApp或者TestFlight中去看哪些機器使用的哪個版本,可以提升效率。
下面是我如何Get這個技能的:
首先,獲取想展示到圖標上的信息,在我的app中,我想展示version,branch,commit信息,即當前測試的App是哪個分支上得哪個版本,在哪次提交之后build的. 這些信息最后是通過shell腳本弄上去的,因此,只要shell腳本能讀取到這些信息就成。對于iOS App來說,Version信息來源于項目的.plist文件,在Mac上提供了PlistBuddy工具來幫助開發者提取plist中的所有信息: 
version = `/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "騷窩對對碰-info.plist"` #1.0
至于branch和commit信息則是根據Version control工具來定的,我使用的是git:
branch=`git rev-parse --abbrev-ref HEAD`commit=`git rev-parse --short HEAD`
接著,有了想寫到icon的信息,接下來是如何寫到icon上,這兒的工具是ImageMagick和Ghostscript,ImageMagick為在命令行下操作圖片提供了很多的功能,而ghostscript則是為了在icon上寫的字體好看一點。在Mac下有了homebrew安裝什么都很方便:
brew install imagemagickbrew install ghostscript
安裝好后,通過ImageMagicK的convert功能把文字寫到圖片上,示例:在Icon.png上,創建一個背景色為‘#0008’的,長144,高40的矩形,然后用白色字體把把“test master 56789998”居中寫到矩形中。
convert -background '#0008' -fill white -gravity center -size 144x40 caption:"test master 56789998" ./Icon.png +swap -gravity south -composite ./convert.png
轉換后的前后對比如下:
最后,有了要寫的信息和如何寫的方式,接下來,就是在Xcode中完成配置,把該功能加入到iOS App的構建過程中。
1.把所有的Icon文件改為*_base.png, 因為最后的Icon文件是由腳本生成,因此需把當前Icon改名以防沖突。 2.在Target的Build Phases中添加一個Run Script的Build Phase, 怎么做:http://www.runscriptbuildphase.com/ 3.把前面的圖片處理過程,添加到Run Script中:
commit=`git rev-parse --short HEAD`branch=`git rev-parse --abbrev-ref HEAD`version=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFOPLIST_FILE}"`function processIcon() { export PATH=$PATH:/usr/local/bin base_file=$1 base_path=`find ${SRCROOT} -name $base_file` if [[ ! -f ${base_path} || -z ${base_path} ]]; then return; fi target_file=`echo $base_file | sed "s/_base//"` target_path="${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/${target_file}" if [ $CONFIGURATION = "Release" ]; then cp ${base_file} $target_path return fi width=`identify -format %w ${base_path}` convert -background '#0008' -fill white -gravity center -size ${width}x40/ caption:"${version} ${branch} ${commit}"/ ${base_path} +swap -gravity south -composite ${target_path}}processIcon "Icon_base.png"processIcon "Icon@2x_base.png"processIcon "Icon-72_base.png"processIcon "Icon-72@2x_base.png"最后,在我的Simiulatro上得到的效果:
新聞熱點
疑難解答