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 class="hljs json" style="box-sizing: border-box; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;font-size:undefined; padding: 0px; color: rgb(0, 0, 0); background-color: rgb(246, 246, 246) !important; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; margin: 0px; white-space: pre-wrap; display: block; overflow-x: auto; border: 0px; vertical-align: baseline; background-position: initial initial; background-repeat: initial initial;"><span style="box-sizing: border-box; line-height: 25.200000762939453px;"><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">[[[</span><span class="typ" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; color: rgb(43, 145, 175); vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">NSBundle</span><span class="pln" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"> mainBundle</span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">]</span><span class="pln" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"> infoDictionary</span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">]</span><span class="pln" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"> objectForKey</span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">:@</span><span class="str" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; color: rgb(128, 0, 0); vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"><span class="hljs-string" style="box-sizing: border-box; color: rgb(0, 128, 0); font-weight: bold; line-height: 25.200000762939453px;">"CFBundleShortVersionString"</span></span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">]</span></span></code>
<code class="hljs css" style="box-sizing: border-box; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;font-size:undefined; padding: 0px; color: rgb(0, 0, 0); background-color: rgb(246, 246, 246) !important; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; margin: 0px; white-space: pre-wrap; display: block; overflow-x: auto; border: 0px; vertical-align: baseline; background-position: initial initial; background-repeat: initial initial;"><span style="box-sizing: border-box; line-height: 25.200000762939453px;"><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"><span class="hljs-selector-attr" style="box-sizing: border-box; line-height: 25.200000762939453px;">[[</span></span><span class="typ" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; color: rgb(43, 145, 175); vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"><span class="hljs-selector-attr" style="box-sizing: border-box; line-height: 25.200000762939453px;">NSBundle</span></span><span class="pln" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"><span class="hljs-selector-attr" style="box-sizing: border-box; line-height: 25.200000762939453px;"> mainBundle</span></span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"><span class="hljs-selector-attr" style="box-sizing: border-box; line-height: 25.200000762939453px;">]</span></span><span class="pln" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"> <span class="hljs-selector-tag" style="box-sizing: border-box; font-weight: bold; color: rgb(0, 0, 128); line-height: 25.200000762939453px;">objectForInfoDictionaryKey</span></span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">:@</span><span class="str" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; color: rgb(128, 0, 0); vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold; color: rgb(0, 0, 128); line-height: 25.200000762939453px;">"CFBundleShortVersionString"</span></span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold; color: rgb(0, 0, 128); line-height: 25.200000762939453px;">];</span></span></span></code>

获得build号:
<code class="hljs json" style="box-sizing: border-box; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;font-size:undefined; padding: 0px; color: rgb(0, 0, 0); background-color: rgb(246, 246, 246) !important; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; margin: 0px; white-space: pre-wrap; display: block; overflow-x: auto; border: 0px; vertical-align: baseline; background-position: initial initial; background-repeat: initial initial;"><span style="box-sizing: border-box; line-height: 25.200000762939453px;"><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">[[[</span><span class="typ" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; color: rgb(43, 145, 175); vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">NSBundle</span><span class="pln" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"> mainBundle</span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">]</span><span class="pln" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"> infoDictionary</span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">]</span><span class="pln" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"> objectForKey</span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">:@</span><span class="str" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; color: rgb(128, 0, 0); vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;"><span class="hljs-string" style="box-sizing: border-box; color: rgb(0, 128, 0); font-weight: bold; line-height: 25.200000762939453px;">"CFBundleVersion"</span></span><span class="pun" style="box-sizing: border-box; line-height: 25.200000762939453px; border: 0px; padding: 0px; margin: 0px; background-image: none; background-attachment: scroll; background-color: transparent; vertical-align: baseline; background-position: 0% 0%; background-repeat: repeat repeat;">]</span></span></code>

转载链接:

[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值