关于 App的版本号设置

在 Xcode 中 App 的版本号有两个:

version

应用的版本号,对应CFBundleShortVersionString    

build

每builder 加一

在项目->targets->Identity 下可以看到这两个版本号

在代码中获取版本号的方法:

let infoDictionary = NSBundle.mainBundle().infoDictionary
        let appversion : AnyObject? = infoDictionary! ["CFBundleShortVersionString"] as! String
        let buildNumber : AnyObject? = infoDictionary! ["CFBundleVersion"]

那么怎么实现 build 的自动增加呢?

1.点击项目-> Build Phases->New Run Script Phase, 在下面就会多出来一个 RunScript
2.在 shell字段下面的黑框中填写脚本的路径. 这里的是:

${PROJECT_DIR}/tools/buildNumber.sh "${PROJECT_DIR}/${INFOPLIST_FILE}"

其中PROJECT_DIR只 xcode 中项目所在的目录,即项目的xcodeproj文件所在目录, 在这个目录下新增一个目录存放shell 脚本.

3.vi buildNumber.sh

#!/bin/sh

if [ $# -ne 1 ]; then
    echo usage: $0 plist-file
    exit 1
fi

plist="$1"
dir="$(dirname "$plist")"

# Only increment the build number if source files have changed
if [ -n "$(find "$dir" \! -path "*xcuserdata*" \! -path "*.git" -newer "$plist")" ]; then
    buildnum=$(/usr/libexec/Plistbuddy -c "Print CFBundleVersion" "$plist")
    if [ -z "$buildnum" ]; then
        echo "No build number in $plist"
        exit 2
    fi
    buildnum=$(expr $buildnum + 1)
    /usr/libexec/Plistbuddy -c "Set CFBundleVersion $buildnum" "$plist"
    echo "Incremented build number to $buildnum"
else
    echo "Not incrementing build number as source files have not changed"
fi

并赋权

chmod 777 buildNumber.sh

4.随便修改一个项目文件,重新 build, 这时就会看到 build 号在自动每次加一.

2015-08-09 03:005