2011-09-30 47 views
125

我希望能夠將我的iOS項目/應用程序的當前版本作爲NSString對象獲取,而無需在某處某個文件中定義常量。我不想在兩個地方更改我的版本值。如何在代碼中獲取我的iOS項目的當前版本?

當我在項目目標摘要中碰撞我的版本時,需要更新該值。

回答

330

你可以得到的版本號和編號如下:

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

您可以使用此[ [一個NSBundle mainBundle] objectForInfoDictionaryKey:@ 「CFBundleVersion」]代替[[一個NSBundle mainBundle] objectForInfoDictionaryKey:(的NSString *)kCFBundleVersionKey]。前者似乎與韻以及[一個NSBundle mainBundle] objectForInfoDictionaryKey:@ 「CFBundleShortVersionString」];所以現在它已經成爲很容易保持在內存中 –

+4

@Duraiamuthan我推薦使用'(* NSString的)kCFBundleVersionKey'而不是'@「CFBundleVersion」'。即使鍵入的時間更長(自動完成將有所幫助),因爲它是一個常量,它在編譯時會被檢查。 –

+0

順便說一句,這將是更準確的做出'NSBundle'這個擴展,而不是'UIApplication'這也將允許替換實例那些靜態調用。 – Zapko

2

在斯威夫特,您可以通過使用得到包版本:

let info:NSDictionary = NSBundle.mainBundle().infoDictionary! 

let version:String = info.objectForKey("CFBundleShortVersionString") as! String 

versionLabel.text = "Version:" + version 
1

的我的開源項目,App Update Tracker

  • + (NSString *)getShortVersionString
  • + (NSString *)getLongVersionString

你會使用它像這樣:在類方法的形式提供了這個功能(及以上)

#import "AppUpdateTracker.h" 

NSLog(@"short version: %@", [AppUpdateTracker getShortVersionString]); 
NSLog(@"long version: %@", [AppUpdateTracker getLongVersionString]); 
0

因此,繼承人一個斯威夫特版本這兩個分別:

let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String 
let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String 

其包括在此回購,檢查出來:

https://github.com/goktugyil/EZSwiftExtensions

+0

CFBundleVersionKey不再適用於Xcode中8和斯威夫特3.你知道新的關鍵? – Crashalot

+0

這裏是一個長達一個日期:https://github.com/goktugyil/EZSwiftExtensions/blob/b06d55a8f5d4fda9f5bca03af4863252dcf53e10/Sources/EZSwiftFunctions.swift – Esqarrouth

2

在Objective C:

1)爲了得到應用的版本,你必須使用一個:

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; 

2 )爲了得到構建的版本,你必須使用一個:

NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; 
11

下面是對Xcode中8的工作,斯威夫特3:

let gAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0" 
let gAppBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0" 

print("Version: \(gAppVersion)") 
print("Build: \(gAppBuild)") 
0

只是做筆記

要獲得任意鍵的本地化值,你應該使用CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), "CFBundleShortVersionString" as CFString)

相關問題