有没有办法以编程方式获取我的应用程序的构建版本?我需要能够检测用户何时通过AppStore更新了应用,以便执行一些代码进行调整
另请参见stackoverflow.com/questions/7608632/...
—
亚历克斯·诺拉斯科
有没有办法以编程方式获取我的应用程序的构建版本?我需要能够检测用户何时通过AppStore更新了应用,以便执行一些代码进行调整
Answers:
您在Xcode目标摘要的“版本”字段中设置的值位于此处:
迅捷3
let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
对象
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
迅捷2
let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
和“构建”:
迅捷3
let build = Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String
对象
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
迅捷2
let build = NSBundle.mainBundle().infoDictionary?[kCFBundleVersionKey as String] as! String
NSBundle.mainBundle()
现在是Bundle.main
您可以尝试使用infoDictionary
NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary];
NSString *version = infoDictionary[@"CFBundleShortVersionString"];
NSString *build = infoDictionary[(NSString*)kCFBundleVersionKey];
NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey];
使用时 MFMailComposeViewController
一个“联系我们”按钮,我用这个代码来获得一个使用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];
这是收到消息时的结果:
两者的Swift版本分别:
let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String
它包含在此仓库中,请查看:
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)") }
项目设定
1)要获取App版本,您必须使用:
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
2)要获得Build版本,您必须使用:
NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];