我试图访问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)
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之后的字符在这里很重要
Chr*_*ris 12
很好的方式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))."
}
}
Run Code Online (Sandbox Code Playgroud)
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)
这是获取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)
如果有任何问题,请告诉我.这对我有用.
在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)
//返回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)
| 归档时间: |
|
| 查看次数: |
58136 次 |
| 最近记录: |