iOS app, programmatically get build version

IosObjective C

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

You can try using the infoDictionary

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

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

Solution 3 - Ios

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:

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:

enter image description here

Solution 4 - Ios

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:

https://github.com/goktugyil/EZSwiftExtensions

Solution 5 - Ios

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"];

Solution 6 - Ios

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

enter image description here

Solution 7 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjcardeneteView Question on Stackoverflow
Solution 1 - Iose1985View Answer on Stackoverflow
Solution 2 - IosAnupdasView Answer on Stackoverflow
Solution 3 - IosneowinstonView Answer on Stackoverflow
Solution 4 - IosEsqarrouthView Answer on Stackoverflow
Solution 5 - IosVaibhav ShiledarView Answer on Stackoverflow
Solution 6 - IosVasily BodnarchukView Answer on Stackoverflow
Solution 7 - IosStunnerView Answer on Stackoverflow