iOS 代码实现获得应用的版本号(Version/Build)

[1]概念

iOS的版本号,一个叫做Version,一个叫做Build,这两个值都可以在Xcode 中选中target,点击“Summary”后看到。 Version在plist文件中的key是“CFBundleShortVersionString”,和AppStore上的版本号保持一致,Build在plist中的key是“CFBundleVersion”,代表build的版本号,该值每次build之后都应该增加1。这两个值都可以在程序中通过下面的代码获得:

[[[NSBundle mainBundle] infoDictionary] valueForKey:@"key"]

[2]具体实现

代码实现获得应用的Verison号:

<code style="padding: 0px; margin: 0px; white-space: nowrap; border: 0px; border-radius: 3px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline; background-color: rgb(246, 246, 246);"><span style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; font-size: 14px;"><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">[[[</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(43, 145, 175); border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">NSBundle</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;"> mainBundle</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">]</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;"> infoDictionary</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">]</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;"> objectForKey</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">:@</span><span class="str" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(128, 0, 0); border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">"CFBundleShortVersionString"</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">]</span></span></code>
<code style="padding: 0px; margin: 0px; white-space: nowrap; border: 0px; border-radius: 3px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline; background-color: rgb(246, 246, 246);"><span style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; font-size: 14px;"><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">[[</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(43, 145, 175); border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">NSBundle</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;"> mainBundle</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">]</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;"> objectForInfoDictionaryKey</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">:@</span><span class="str" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(128, 0, 0); border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">"CFBundleShortVersionString"</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; border: 0px; vertical-align: baseline; background: none 0% 0% repeat scroll transparent;">];</span></span></code>

获得build号:

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

转载链接:http://www.open-open.com/lib/view/open1411817778203.html

经验来自:

[1]http://joeyio.com/ios/2013/05/25/xcode-version-management/

[2]http://stackoverflow.com/questions/7957794/how-to-get-your-own-app-version-from-xcode

 [ 3 ]http://blog.csdn.net/linzhiji/article/details/8173503




你可能感兴趣的:(iOS 代码实现获得应用的版本号(Version/Build))