2011-10-31 96 views
35

我想知道在將代碼放入xCode中的「摘要」選項卡後,是否有辦法在代碼中獲取您自己的應用程序版本。一種方法似乎是搜索Info.plist的關鍵,但有沒有其他更簡單,更方便的方法?如何從xcode獲取自己的應用程序版本?

+0

[我的iPhone應用程序如何檢測自己的版本號?]的可能重複?(http://stackoverflow.com/questions/458632/how-can-my-iphone-app-detect-its-own-version-number) –

回答

113

您可以在主包中找到它。我認爲這是這樣的:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; 
+0

是的,我剛剛發現了一個博客(http://pigtailsoft.com/blog/?p=35)。不管怎麼說,還是要謝謝你!將在6分鐘內接受答案:) –

+11

謝謝,但'@「CFBundleVersion」'返回版本不是版本。你應該得到這個對象:'@「CFBundleShortVersionString」' – IgniteCoders

12
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; 
27

我通常使用的內部版本號(CFBundleVersion)來跟蹤的,那麼,數量構建,以及在市場推廣的版本號(CFBundleShortVersionString)。我使用運行腳本來自動增加內部版本號,並在每次發佈新版本之前手動更新版本號。所以,如果你想在你的代碼的實際版本號,而不是內部版本號,使用:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] 

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; 

下面是遞增的內部版本號爲任何有興趣的運行腳本:

#!/bin/bash 

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}" 
2

對於斯威夫特:

Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") 
相關問題