iOS 应用程序,以编程方式获取构建版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16888780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS app, programmatically get build version
提问by jcardenete
Is there a way to programmatically get the build version of my app? I need to be able to detect when the user has updated the app through the AppStore, in order to execute some code for adjustments
有没有办法以编程方式获取我的应用程序的构建版本?我需要能够检测到用户何时通过 AppStore 更新了应用程序,以便执行一些代码进行调整
回答by e1985
The value you set in the Xcode target summary's "Version" field is in here:
您在 Xcode 目标摘要的“版本”字段中设置的值在这里:
Swift 3
斯威夫特 3
let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
ObjC
对象
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
Swift 2
斯威夫特 2
let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
and the "Build":
和“构建”:
Swift 3
斯威夫特 3
let build = Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String
ObjC
对象
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
Swift 2
斯威夫特 2
let build = NSBundle.mainBundle().infoDictionary?[kCFBundleVersionKey as String] as! String
回答by Anupdas
You can try using the infoDictionary
您可以尝试使用 infoDictionary
NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary];
NSString *version = infoDictionary[@"CFBundleShortVersionString"];
NSString *build = infoDictionary[(NSString*)kCFBundleVersionKey];
NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey];
回答by neowinston
When using MFMailComposeViewController
for a "Contact Us" button, I use this code to get a formatted HTML on the e-mail from the user. It gives me all the info that I need to start helping solving any issue:
当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];
This is the result when getting the message:
这是收到消息时的结果:
回答by Esqarrouth
Swift version for both separately:
Swift 版本分别用于:
let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String
Its included in this repo, check it out:
它包含在此 repo 中,请查看:
回答by Vasily Bodnarchuk
Details
细节
- Xcode Version 10.3 (10G8), Swift 5
- Xcode 版本 10.3 (10G8),Swift 5
Solution
解决方案
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 }
}
Usage
用法
if let value = Info.Value.version.string { print("Version: \(value)") }
if let value = Info.Value.build.string { print("Build: \(value)") }
Result
结果
Project settings
项目设置
回答by Vaibhav Shiledar
1)For getting App version you have to use a:
1)要获得应用程序版本,您必须使用:
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
2)For getting Build version you have to use a:
2)要获得 Build 版本,您必须使用:
NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
回答by Stunner
I have written this open source projectfor precisely this purpose. My project posts notifications when significant events occur, such as when a user opens the app for the first time, or opens the app after upgrading (complete with information on which version the user upgraded from). The source is straightforward and should be easy to understand. Let me know if you have any questions/requests.
我正是为此目的编写了这个开源项目。我的项目在发生重大事件时发布通知,例如当用户第一次打开应用程序时,或在升级后打开应用程序(完成有关用户从哪个版本升级的信息)。来源很简单,应该很容易理解。如果您有任何问题/要求,请告诉我。
I have also recently written a blog postabout it.