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

Ken*_*Dex 80 ios swift

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

Con*_*nor 144

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

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

  • 为什么这会被贬低?我想它可能更自然地作为'let`,但它看起来是正确的. (2认同)
  • 这似乎打破了XCode 6 beta 3 (2认同)
  • 这在Xcode 6 beta 5中对我有用:`let text = NSBundle.mainBundle().infoDictionary ["CFBundleVersion"]!as String` (2认同)

Dan*_*ark 103

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)"
} 
Run Code Online (Sandbox Code Playgroud)

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)"
}
Run Code Online (Sandbox Code Playgroud)

如看到这里.


Jul*_*anM 17

对于Xcode 6的最终版本使用

NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String
Run Code Online (Sandbox Code Playgroud)

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

  • 该死。我确实喜欢这整个“可选?,打开!” 系统,非常清晰易读! (3认同)

Chr*_*ris 12

很好的方式AppName,AppVersionBuildNumber......

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))."
   }
}
Run Code Online (Sandbox Code Playgroud)

  • `Bundle.main` 有一个空的 `infoDictionary` 给我;也许是因为我是在框架内而不是可执行文件或应用程序中执行此操作?`Bundle(for: MyClass.self)` 包含预期值。 (2认同)

Luk*_*ker 12

斯威夫特 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
     }
}
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

print("The apps name = \(AppInfo.appname)")
Run Code Online (Sandbox Code Playgroud)

  • 应用程序名称也可以是“CFBundleDisplayName” (2认同)

Ash*_*shu 9

这是获取Build和版本的简单方法.

对于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)
   }
Run Code Online (Sandbox Code Playgroud)

对于Objective C

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

NSString * currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
Run Code Online (Sandbox Code Playgroud)

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


Jir*_*cak 7

在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))"
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以使用以下来获得你需要的一切:

let version = UIApplication.sharedApplication.applicationVersion() // 1
let build = UIApplication.sharedApplication.applicationBuild() // 80
let both = UIApplication.sharedApplication.versionBuild() // 1(80)
Run Code Online (Sandbox Code Playgroud)


Pra*_*tik 7

//返回app的版本号

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

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

public static var appBuild: String? {
    return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String
}
Run Code Online (Sandbox Code Playgroud)