ios 使用 Swift 获取版本和构建信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24501288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:10:59  来源:igfitidea点击:

Getting Version and Build Info with Swift

iosswift

提问by Ken-UbiDex

I am attempting to gain access to the Main NSBundle to retrieve Version and Build information. Thing is, I want to try it in swift, I know how to retrieve it in Objective-C with Build.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];. Yet I don't know where to start with swift, I have attempted to write it in the new syntax with no avail.

我正在尝试访问 Main NSBundle 以检索版本和构建信息。问题是,我想快速尝试一下,我知道如何在 Objective-C 中使用Build.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];. 但是我不知道从哪里开始 swift,我试图用新的语法编写它,但无济于事。

回答by Connor

What was wrong with the Swift syntax? This seems to work:

Swift 语法有什么问题?这似乎有效:

if let text = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
    print(text)
}

回答by Dan Rosenstark

Swift 3/4 Version

斯威夫特 3/4 版本

func version() -> String {
    let dictionary = Bundle.main.infoDictionary!
    let version = dictionary["CFBundleShortVersionString"] as! String
    let build = dictionary["CFBundleVersion"] as! String
    return "\(version) build \(build)"
} 


Swift 2.x Version

Swift 2.x 版本

func version() -> String {
    let dictionary = NSBundle.mainBundle().infoDictionary!
    let version = dictionary["CFBundleShortVersionString"] as String
    let build = dictionary["CFBundleVersion"] as String
    return "\(version) build \(build)"
}

as seen here.

这里所见。

回答by JulianM

For the final release of Xcode 6 use

对于 Xcode 6 的最终版本,请使用

NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String

The "?" character after infoDictionary is important here

这 ”?” infoDictionary 之后的字符在这里很重要

回答by Ashu

Here is simple way to get Build and version.

这是获取构建和版本的简单方法。

For Swift 4.X

对于 Swift 4.X

 if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
     print(version)
   }

 if let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
     print(build)
   }

For Objective C

对于目标 C

NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

NSString * currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

Let me know if any issue. This is working for me.

如果有任何问题,请告诉我。这对我有用。

回答by Chris

Swifty way for AppName, AppVersionand BuildNumber...

快速的方式AppNameAppVersionBuildNumber...

if let dict = NSBundle.mainBundle().infoDictionary {
   if let version = dict["CFBundleShortVersionString"] as? String,
       let bundleVersion = dict["CFBundleVersion"] as? String,
       let appName = dict["CFBundleName"] as? String {
           return "You're using \(appName) v\(version) (Build \(bundleVersion))."
   }
}

回答by Jiri Trecak

In swift, I would make it as extension for UIApplication, like this:

在 swift 中,我会将其作为 UIApplication 的扩展,如下所示:

extension UIApplication {

    func applicationVersion() -> String {

        return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
    }

    func applicationBuild() -> String {

        return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
    }

    func versionBuild() -> String {

        let version = self.applicationVersion()
        let build = self.applicationBuild()

        return "v\(version)(\(build))"
    }
}

Then you can just use following to get everything you need:

然后您可以使用以下内容来获取您需要的一切:

let version = UIApplication.sharedApplication.applicationVersion() // 1
let build = UIApplication.sharedApplication.applicationBuild() // 80
let both = UIApplication.sharedApplication.versionBuild() // 1(80)

回答by Pratyush Pratik

//Returns app's version number

//返回应用的版本号

public static var appVersion: String? {
    return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}

//Return app's build number

//返回应用程序的内部版本号

public static var appBuild: String? {
    return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String
}

回答by LukeSideWalker

Swift 5.0

斯威夫特 5.0

I created a wrapper for Swift 5 to get some app related stringsat one place in all my apps, called AppInfo.

我为 Swift 5 创建了一个包装器,以便在我所有应用程序的一个地方获取一些与应用程序相关的字符串,称为AppInfo

struct AppInfo {

var appName : String {
    return readFromInfoPlist(withKey: "CFBundleName") ?? "(unknown app name)"
}

var version : String {
    return readFromInfoPlist(withKey: "CFBundleShortVersionString") ?? "(unknown app version)"
}

var build : String {
    return readFromInfoPlist(withKey: "CFBundleVersion") ?? "(unknown build number)"
}

var minimumOSVersion : String {
    return readFromInfoPlist(withKey: "MinimumOSVersion") ?? "(unknown minimum OSVersion)"
}

var copyrightNotice : String {
    return readFromInfoPlist(withKey: "NSHumanReadableCopyright") ?? "(unknown copyright notice)"
}

var bundleIdentifier : String {
    return readFromInfoPlist(withKey: "CFBundleIdentifier") ?? "(unknown bundle identifier)"
}

var developer : String { return "my awesome name" }

// lets hold a reference to the Info.plist of the app as Dictionary
private let infoPlistDictionary = Bundle.main.infoDictionary

/// Retrieves and returns associated values (of Type String) from info.Plist of the app.
private func readFromInfoPlist(withKey key: String) -> String? {
    return infoPlistDictionary?[key] as? String
     }
}

You can use it like so:

你可以像这样使用它:

print("The apps name = \(AppInfo.appname)")

回答by Tarika Chawla

For Swift 3,Replace NSBundle with Bundle and mainBundle is replaced simply by main.

对于 Swift 3,将 NSBundle 替换为 Bundle 并且 mainBundle 被简单地替换为 main。

let AppVersion = Bundle.main.infoDictionary!["CFBundleVersion"] as! String

回答by Crashalot

This code works for Swift 3, Xcode 8:

此代码适用于 Swift 3、Xcode 8:

let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"