前言:
1.记录xcode编辑次数很有必要,特别是在频繁发版本时和根据现有编译次数记录估算工期时间很有帮助
2.全部自动化处理,告别手动时代
正文:
1.新建工程或者现有工程里设置:
然后设置xcode-build-bump.sh 和 xcode-version-bump.sh脚本
xcode-build-bump.sh
# xcode-build-bump.sh # @desc Auto-increment the build number every time the project is run. # @usage # 1. Select: your Target in Xcode # 2. Select: Build Phases Tab # 3. Select: Add Build Phase -> Add Run Script # 4. Paste code below in to new "Run Script" section # 5. Drag the "Run Script" below "Link Binaries With Libraries" # 6. Insure that your starting build number is set to a whole integer and not a float (e.g. 1, not 1.0) buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}") buildNumber=$(($buildNumber + 1)) /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"
xcode-version-bump.sh
# xcode-version-bump.sh # @desc Auto-increment the version number (only) when a project is archived for export. # @usage # 1. Select: your Target in Xcode # 2. Select: Build Phases Tab # 3. Select: Add Build Phase -> Add Run Script # 4. Paste code below in to new "Run Script" section # 5. Check the checkbox "Run script only when installing" # 6. Drag the "Run Script" below "Link Binaries With Libraries" # 7. Insure your starting version number is in SemVer format (e.g. 1.0.0) # This splits a two-decimal version string, such as "0.45.123", allowing us to increment the third position. VERSIONNUM=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}") NEWSUBVERSION=`echo $VERSIONNUM | awk -F "." '{print $3}'` NEWSUBVERSION=$(($NEWSUBVERSION + 1)) NEWVERSIONSTRING=`echo $VERSIONNUM | awk -F "." '{print $1 "." $2 ".'$NEWSUBVERSION'" }'` /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $NEWVERSIONSTRING" "${PROJECT_DIR}/${INFOPLIST_FILE}"
接下来看看编译运行结果:
中途可以任意更改version 和 build,不过只要保持格式一致就可以了。
然后工程里面使用:
#define F(string, args...) [NSString stringWithFormat:string, args] @interface TKViewController () @property (weak, nonatomic) IBOutlet UILabel *labelVersion; @end @implementation TKViewController //1.0.0 - (NSString *)bundleShortVersionString{ static NSString *key = @"CFBundleShortVersionString"; return [[NSBundle mainBundle] objectForInfoDictionaryKey:key]; } //2938 - (NSString *)bundleBuildVersionString{ return [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]; } //3.0.0(2938) - (NSString *)bundleFullVersionString{ NSString *version = [self bundleShortVersionString]; NSString *build = [self bundleBuildVersionString]; return F(@"%@(%@)", version, build); } - (void)viewDidLoad { [super viewDidLoad]; self.labelVersion.text = F(@"version %@",[self bundleFullVersionString]); // Do any additional setup after loading the view, typically from a nib. }
结果图:
注意:
我的环境是在xcode5.1.1下编译没问题。
Xcode 4.6.3 (4H1503) 会出现:
/Users/tinkl/Library/Developer/Xcode/DerivedData/ZwingIt-glpdhlyoebaqcfdefdfwkgxjapnj/Build/Intermediates/ZwingIt.build/Debug-iphoneos/ZwingIt.build/Script-048F0D0017A7FDD9000E38C5.sh: line 3: File Doesn't Exist, Will Create: /Users/nebiros/Dropbox/Projects/zwingit-ios-2//Users/nebiros/Dropbox/Projects/zwingit-ios-2/ZwingIt-Info.plist + 1: syntax error: invalid arithmetic operator (error token is "'t Exist, Will Create: /Users/nebiros/Dropbox/Projects/zwingit-ios-2//Users/nebiros/Dropbox/Projects/zwingit-ios-2/ZwingIt-Info.plist + 1")
xcode5.0.1没问题