如何使用Swift获取App版本和内部版本号? [英] How do I get the App version and build number using Swift?

查看:348
本文介绍了如何使用Swift获取App版本和内部版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Azure后端的IOS应用程序,并希望记录某些事件,例如登录以及用户正在运行的应用程序版本。

I have an IOS app with an Azure back-end, and would like to log certain events, like logins and which versions of the app users are running.

如何使用Swift返回版本和内部版本号?

How can I return the version and build number using Swift?

推荐答案

编辑

正如@azdev在Xcode的新版本中所指出的那样,你会在尝试我之前的解决方案时遇到编译错误,要解决这个问题,只需按照建议进行编辑即可使用!来解包捆绑字典!

As pointed out by @azdev on the new version of Xcode you will get a compile error for trying my previous solution, to solve this just edit it as suggested to unwrap the bundle dictionary using a !

let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]

结束编辑

只需使用与Objective-C相同的逻辑,但只需进行一些小改动

Just use the same logic than in Objective-C but with some small changes

//First get the nsObject by defining as an optional anyObject
let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary["CFBundleShortVersionString"]

//Then just cast the object as a String, but be careful, you may want to double check for nil
let version = nsObject as String

我希望这能帮到你。

David

这篇关于如何使用Swift获取App版本和内部版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆