[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>
<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