iOS应用程序,编程获取构build版本
有没有办法以编程方式获取我的应用程序的构build版本? 我需要能够检测用户何时通过AppStore更新应用程序,以执行一些代码进行调整
在Xcode目标摘要的“版本”字段中设置的值在这里:
Swift 3
let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
ObjC
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
Swift 2
let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
和“Build”:
Swift 3
let build = Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String
ObjC
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
Swift 2
let build = NSBundle.mainBundle().infoDictionary?[kCFBundleVersionKey as String] as! String
你可以尝试使用infoDictionary
NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary]; NSString *version = infoDictionary[@"CFBundleShortVersionString"]; NSString *build = infoDictionary[(NSString*)kCFBundleVersionKey]; NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey];
当使用MFMailComposeViewController
作为“联系我们”button时,我使用此代码从用户的电子邮件中获取格式化的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 bellow:", 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
它包括在这个回购,检查出来:
Swift 3获取包版本并构buildstring
码
extension UIApplication { class var Build: String? { get { return Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String } } class var Version: String? { get { return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String } } }
用法
print("Version \(UIApplication.Version!)") print("Build \(UIApplication.Build!)")
结果
项目设置
安慰
我已经为此写了这个开源项目 。 我的项目在重大事件发生时发布通知,例如用户首次打开应用程序,或在升级后打开应用程序(完成用户从哪个版本升级的信息)。 来源很简单,应该很容易理解。 如果您有任何问题/要求,请告知我。
我最近也写了一篇关于它的博客文章 。
1)获取应用程序版本,你必须使用:
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
2)获取生成版本,你必须使用:
NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];