Vous pouvez obtenir la version et construire les numéros comme suit:
NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];
J'ai les méthodes suivantes dans une catégorie sur UIApplication
:
+ (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;
}
Voici l'équivalent dans Swift:
extension UIApplication {
class func appVersion() -> String {
return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as String
}
class func appBuild() -> String {
return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as NSString) as String
}
class func versionBuild() -> String {
let version = appVersion(), build = appBuild()
return version == build ? "v\(version)" : "v\(version)(\(build))"
}
}