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
7 Answers
The value you set in the Xcode target summary's "Version" field is in here:
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 and the "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 3 Comments
NSBundle.mainBundle() is now Bundle.mainYou can try using the infoDictionary
NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary]; NSString *version = infoDictionary[@"CFBundleShortVersionString"]; NSString *build = infoDictionary[(NSString*)kCFBundleVersionKey]; NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey]; 1 Comment
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:
#import <sys/utsname.h> // Don't forget to import that somewhere // 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:
Comments
Swift version for both separately:
let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String Its included in this repo, check it out:
1 Comment
1)For getting App version you have to use a:
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; 2)For getting Build version you have to use a:
NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; Comments
Details
- Xcode Version 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
Comments
I have written this open source project for 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 post about it.

