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

Ios 使用Swift获取版本和构建信息,ios,swift,objective-c,nsbundle,Ios,Swift,Objective C,Nsbundle,我正在尝试访问主NSBundle,以检索版本和构建信息。问题是,我想在Swift中尝试它,我知道如何在Objective-C中检索它: text=[NSBundle.mainBundle.infoDictionary objectForKey:@“CbundLeverVersion”]; 但是我不知道从哪里开始使用Swift,我尝试用新语法编写它,但没有用。Swift语法有什么问题?这似乎有效: if let text = Bundle.main.infoDictionary?["CFBund

我正在尝试访问主
NSBundle
,以检索版本和构建信息。问题是,我想在Swift中尝试它,我知道如何在Objective-C中检索它:

text=[NSBundle.mainBundle.infoDictionary objectForKey:@“CbundLeverVersion”];

但是我不知道从哪里开始使用Swift,我尝试用新语法编写它,但没有用。

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

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

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

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

信息字典后面的“?”字符在这里很重要

Swift 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)"
} 
func version() -> String {
    let dictionary = NSBundle.mainBundle().infoDictionary!
    let version = dictionary["CFBundleShortVersionString"] as String
    let build = dictionary["CFBundleVersion"] as String
    return "\(version) build \(build)"
}

Swift 2.x版

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

如图所示。

[Update:Xcode 6.3.1]我尝试了上述所有方法,但在Xcode 6.3.1中没有一种方法可以奏效,但我发现:

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

在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))"
    }
}
然后,您可以使用以下方法获得所需的一切:

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

另一个选项是在AppDelegate中定义变量:

var applicationVersion:String {
    return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
}
var applicationBuild:String  {
    return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
}
var versionBuild:String  {
    let version = self.applicationVersion
    let build = self.applicationBuild
    return "version:\(version) build:(\(build))"
}

可在AppDelegate中作为变量引用的
AppName
AppVersion
BuildNumber
的快捷方式

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))."
   }
}

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

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

let textVersion
 = Bundle.main.infoDictionary?["CFBundleVersion"] as? String

下面是获取构建和版本的简单方法

适用于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)
   }
针对目标C

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

NSString * currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
如果有任何问题,请告诉我。这对我有用。

SWIFT 3版本

if let infoPath = Bundle.main.path(forResource: "Info.plist", ofType: nil),
        let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath),
        let infoDate = infoAttr[.creationDate] as? Date
{
    return infoDate
}
return Date()

对于Swift 3,将NSBundle替换为Bundle,mainBundle仅替换为main

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

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

public static var appVersion: String? {
    return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
//返回应用程序的内部版本号

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

要获得您可以使用的框架的结果

let version = Bundle(for: type(of: self)).infoDictionary?["CFBundleShortVersionString"] as? String
还是在测试中

let version = Bundle(for: <SomeClass>.self).infoDictionary?["CFBundleShortVersionString"] as? String
let version=Bundle(for:.self).infoDictionary?[“CbundleShortVersionString”]as?一串

Swift 5.0

我为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
     }
}
您可以这样使用它:

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


展示你已经做过的尝试。它与Objective-C实现非常相似。2017年1月swift 2.3:让sVer=NSBundle.mainBundle().infoDictionary?[“CbundleShortVersionString”]as?字符串let sBuild=NSBundle.mainBundle().infoDictionary?[“CbundLeverVersion”]as?String self.myVersionLabel.text=String(格式:“Version%@Build%@”,sVer!,sBuild!)为什么会被否决?我想它作为
let
可能更自然,但它看起来是正确的。啊,我忘记了“as String”,所以变量会接受它作为字符串。谢谢。这似乎在XCode 6 beta 3中被打破了。这在XCode 6 beta 5中对我有效:
let text=NSBundle.mainBundle().infoDictionary[“CbundLeverVersion”]!作为String
@Dean,看起来他们更改了infoDictionary以返回可选的。我已经对答案进行了编辑。如果您两次获得相同的密钥,则应使用“CbundLeverVersion”作为版本。我猜这是一个副本/过去的打字错误:)谢谢@foOg这是一个打字错误。事实上,它是反向的:短的是版本,常规的是构建。奇怪,我知道。斯威夫特3版。如果让infoPath=Bundle.main.path(forResource:“Info.plist”,类型为nil),则让infoAttr=try?FileManager.default.attributesOfItem(atPath:infoPath),是否将infoDate=infoAttr[.creationDate]设为?日期{return infoDate}返回日期()。我真的很喜欢这个“可选”的“展开”系统,它非常清晰易读!为什么需要展开这些值?据我所知,这些价值观中的任何一个都不可能实现nil@Michael有些人总是打开可选值。有人说你是对的。
Bundle.main
为我准备了一本空的
infoDictionary
;也许是因为我是在一个框架内完成的,而不是一个可执行文件或应用程序<代码>捆绑包(for:MyClass.self)包含预期值。在Swift中,值不可用时,使用nil。在我看来,您应该返回nil而不是自定义占位符字符串。这就是为什么它是一个包装器:总是返回一些东西。零的情况并没有真正“被摧毁”,它仍然存在,但已经得到了预先处理。否则,您将不得不在应用程序的另一个位置处理“零”情况。谢谢。另外,最后一个大括号位于代码块之外。:)应用程序名称也可以是
CbundleDisplayName