IOS 应用程序,通过编程获得构建版本

有没有一种方法可以通过编程获得我的应用程序的构建版本?我需要能够检测用户何时通过 AppStore 更新了应用程序,以便执行一些调整代码

75741 次浏览

您可以尝试使用 infoDictionary

NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary];


NSString *version = infoDictionary[@"CFBundleShortVersionString"];
NSString *build = infoDictionary[(NSString*)kCFBundleVersionKey];
NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey];

在 Xcode 目标摘要的“ Version”字段中设置的值在这里:

Swift 3

let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String

目的

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

Swift 2

let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String

以及“建造”:

Swift 3

let build = Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String

目的

NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];

Swift 2

let build = NSBundle.mainBundle().infoDictionary?[kCFBundleVersionKey as String] as! String

我写这个 开放源码项目正是为了这个目的。我的项目在重大事件发生时发布通知,比如用户第一次打开应用程序,或者升级后打开应用程序(包括用户从哪个版本升级的信息)。源代码很简单,应该很容易理解。如果你有任何问题/要求,请告诉我。

我最近还写了一个 博客文章关于它。

快速版本:

let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String

它包含在这个回购,检查它:

Https://github.com/goktugyil/ezswiftextensions

当使用 MFMailComposeViewController作为“ Contact Us”按钮时,我使用这段代码从用户的电子邮件中获得一个格式化的 HTML。它给了我所有的信息,我需要开始帮助解决任何问题:

struct utsname systemInfo;
uname(&systemInfo);


NSDateFormatter *formatter = [[NSDateFormatter alloc]  init];
NSDate  *date = [NSDate date];
[formatter setDateFormat:@"MM/dd/yyyy 'at' hh:mm a"];
NSString *dateString = [formatter stringFromDate:date];


CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width - 65.0;


NSString *comments = NSLocalizedString(@"Please write your comments below:", nil);
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];


NSString *emailBody = [NSString stringWithFormat:@"<!DOCTYPE html><html><style> .div {background-color: lightgrey; width: %fpx; padding: 10px; border: 5px solid navy; margin: 2px;}</style><body><div class='div'><p><b>App:</b> %@</p><b><p>Device:</b> %@</p><b><p>iOS Version:</b> %@</p><b><p><p>App Version and Build:</b> %@ (%@)</p><b><p>Date:</b> %@</p> </p></div><font color='red'><b>%@</b></font><br><br></body></html>",screenWidth,appName,deviceModel,iOSVersion,version,build,dateString,comments];


[self setMessageBody:emailBody isHTML:YES];

这就是得到信息的结果:

enter image description here

1)要获得应用程序版本,你必须使用:

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

2)要获得 Build 版本,你必须使用:

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

细节

  • Xcode Version 10.3(10G8) ,Swift 5

解决方案

class Info {
static let dictionary = Bundle.main.infoDictionary ?? [:]
enum Value {
case build, version
}
}


extension Info.Value {


var key: String {
switch self {
case .build: return kCFBundleVersionKey as String
case .version: return kCFBundleInfoDictionaryVersionKey as String
}
}


var string: String? { return Info.dictionary[key] as? String }
}

用法

if let value = Info.Value.version.string { print("Version: \(value)") }
if let value = Info.Value.build.string { print("Build: \(value)") }

结果

项目设置

enter image description here