Jov*_*van 144 iphone objective-c ios swift
我希望能够将我的iOS项目/应用程序的当前版本作为NSString
对象,而无需在某个文件中定义常量.我不想在2个地方更改我的版本值.
当我在Project目标摘要中碰撞我的版本时,需要更新该值.
Ash*_*lls 368
您可以按如下方式获取版本和内部版本号:
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String
Run Code Online (Sandbox Code Playgroud)
或在Objective-C中
NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];
Run Code Online (Sandbox Code Playgroud)
我在类别中有以下方法UIApplication
:
extension UIApplication {
class func appVersion() -> String {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
}
class func appBuild() -> String {
return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String
}
class func versionBuild() -> String {
let version = appVersion(), build = appBuild()
return version == build ? "v\(version)" : "v\(version)(\(build))"
}
}
Run Code Online (Sandbox Code Playgroud)
要点: https ://gist.github.com/ashleymills/6ec9fce6d7ec2a11af9b
这是Objective-C中的等价物:
+ (NSString *) appVersion
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
}
+ (NSString *) build
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];
}
+ (NSString *) versionBuild
{
NSString * version = [self appVersion];
NSString * build = [self build];
NSString * versionBuild = [NSString stringWithFormat: @"v%@", version];
if (![version isEqualToString: build]) {
versionBuild = [NSString stringWithFormat: @"%@(%@)", versionBuild, build];
}
return versionBuild;
}
Run Code Online (Sandbox Code Playgroud)
要点: https ://gist.github.com/ashleymills/c37efb46c9dbef73d5dd
Cra*_*lot 14
以下是适用于Xcode 8,Swift 3的内容:
let gAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
let gAppBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"
print("Version: \(gAppVersion)")
print("Build: \(gAppBuild)")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
64354 次 |
最近记录: |