我希望能够将我的iOS项目/应用的当前版本作为NSString
对象获取,而无需在某个文件中定义常量。我不想在2个地方更改我的版本值。
当我在Project目标摘要中碰撞我的版本时,需要更新该值。
答案 0 :(得分:361)
您可以按如下方式获取版本和内部版本号:
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String
或在Objective-C
NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];
我在UIApplication
上的类别中有以下方法:
extension UIApplication {
class func appVersion() -> String {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
}
class func appBuild() -> String {
return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String
}
class func versionBuild() -> String {
let version = appVersion(), build = appBuild()
return version == build ? "v\(version)" : "v\(version)(\(build))"
}
}
要点: https://gist.github.com/ashleymills/6ec9fce6d7ec2a11af9b
这是Objective-C中的等价物:
+ (NSString *) appVersion
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
}
+ (NSString *) build
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];
}
+ (NSString *) versionBuild
{
NSString * version = [self appVersion];
NSString * build = [self build];
NSString * versionBuild = [NSString stringWithFormat: @"v%@", version];
if (![version isEqualToString: build]) {
versionBuild = [NSString stringWithFormat: @"%@(%@)", versionBuild, build];
}
return versionBuild;
}
要点: https://gist.github.com/ashleymills/c37efb46c9dbef73d5dd
答案 1 :(得分:14)
以下是 Xcode 8,Swift 3:
的功能let gAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
let gAppBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"
print("Version: \(gAppVersion)")
print("Build: \(gAppBuild)")
答案 2 :(得分:2)
在目标C中:
1)要获得App版本,您必须使用:
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
2)要获得Build版本,您必须使用:
NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
答案 3 :(得分:1)
在Swift中,您可以使用以下方法获取包版本:
let info:NSDictionary = NSBundle.mainBundle().infoDictionary!
let version:String = info.objectForKey("CFBundleShortVersionString") as! String
versionLabel.text = "Version:" + version
答案 4 :(得分:0)
我的一个开源项目App Update Tracker以类方法的形式提供此功能(及更多):
+ (NSString *)getShortVersionString
+ (NSString *)getLongVersionString
你会像这样使用它:
#import "AppUpdateTracker.h"
NSLog(@"short version: %@", [AppUpdateTracker getShortVersionString]);
NSLog(@"long version: %@", [AppUpdateTracker getLongVersionString]);
答案 5 :(得分:0)
仅供注意
要获取任何密钥的本地化值,您应使用CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), "CFBundleShortVersionString" as CFString)
答案 6 :(得分:0)
[快速版本:5.2]
所有iOS应用都必须在其Info.plist文件中存储一个应用版本号,但是没有内置的方式可以将其作为可在代码中使用的字符串来获取。
我为UIApplication创建了一个小扩展,该扩展读取了Info.plist文件并自动返回版本号。
这是代码:
extension UIApplication {
static var appVersion: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
}
在控制器内部
@IBOutlet weak var tv_version: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Display Version
setUpCurrentVersion()
}
func setUpCurrentVersion(){
tv_version.text = "v" + UIApplication.appVersion!
}
答案 7 :(得分:-1)