我有一个用Xcode 3开发的应用程序,最近开始用Xcode 4编辑。在目标摘要中,我有带有字段的iOS应用程序目标表单:标识符、版本、构建、设备和部署目标。版本字段是空白的,构建字段是3.4.0(这与我仍然使用Xcode 3编辑应用程序时的版本相匹配)。
我的问题是:
版本和构建字段之间的区别是什么? 为什么我升级到Xcode 4后版本字段是空的?
我有一个用Xcode 3开发的应用程序,最近开始用Xcode 4编辑。在目标摘要中,我有带有字段的iOS应用程序目标表单:标识符、版本、构建、设备和部署目标。版本字段是空白的,构建字段是3.4.0(这与我仍然使用Xcode 3编辑应用程序时的版本相匹配)。
我的问题是:
版本和构建字段之间的区别是什么? 为什么我升级到Xcode 4后版本字段是空的?
当前回答
另一种方法是在appDelegate didFinishLaunchingWithOptions中设置版本号:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString * ver = [self myVersion];
NSLog(@"version: %@",ver);
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:ver forKey:@"version"];
return YES;
}
- (NSString *) myVersion {
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
return [NSString stringWithFormat:@"%@ build %@", version, build];
}
其他回答
另一种方法是在appDelegate didFinishLaunchingWithOptions中设置版本号:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString * ver = [self myVersion];
NSLog(@"version: %@",ver);
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:ver forKey:@"version"];
return YES;
}
- (NSString *) myVersion {
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
return [NSString stringWithFormat:@"%@ build %@", version, build];
}
如果构建号是浮点值,那么上面答案中自动递增构建号的脚本对我不起作用,所以我稍微修改了一下:
#!/bin/bash
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=`echo $buildNumber +1|bc`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"
我在Xcode 14.2上,并将以下bash脚本添加到项目包中的.sh文件中。然后我使用一个运行脚本构建阶段来调用这个脚本。当我存档项目时,版本和包更新了几次,但随后停止了增量。关于为什么脚本不会继续增加有任何反馈吗?感谢反馈。
currentVersionString=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$PROJECT_DIR/SupportingFiles/$INFOPLIST_FILE")
currentBuildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$PROJECT_DIR/SupportingFiles/$INFOPLIST_FILE")
newBuildNumber=$((currentBuildNumber + 1))
IFS='.' read -ra version <<< "$currentVersionString"
version[2]=$((version[2] + 1))
newVersionString="${version[0]}.${version[1]}.${version[2]}"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $newVersionString" "$PROJECT_DIR/SupportingFiles/$INFOPLIST_FILE"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $newBuildNumber" "$PROJECT_DIR/SupportingFiles/$INFOPLIST_FILE"
(只是把这个留在这里供我自己参考。)这将显示你在Xcode目标中看到的"version"和"build"字段的version和build:
- (NSString*) version {
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
return [NSString stringWithFormat:@"%@ build %@", version, build];
}
在斯威夫特
func version() -> String {
let dictionary = NSBundle.mainBundle().infoDictionary!
let version = dictionary["CFBundleShortVersionString"] as? String
let build = dictionary["CFBundleVersion"] as? String
return "\(version) build \(build)"
}
感谢@nekno和@ale84的精彩回答。
但是,我修改了@ale84的脚本,它很少增加浮点的构建号。
incl的值可以根据您的浮动格式要求进行更改。 例如:如果incl = .01,输出格式为 ... 1.19, 1.20, 1.21 ...
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
incl=.01
buildNumber=`echo $buildNumber + $incl|bc`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"