How can my iphone app detect its own version number? How can my iphone app detect its own version number? ios ios

How can my iphone app detect its own version number?


As I describe here, I use a script to rewrite a header file with my current Subversion revision number. That revision number is stored in the kRevisionNumber constant. I can then access the version and revision number using something similar to the following:

[NSString stringWithFormat:@"Version %@ (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"], kRevisionNumber]

which will create a string of the format "Version 1.0 (51)".


Building on Brad Larson's answer, if you have major and minor version info stored in the info plist (as I did on a particular project), this worked well for me:

- (NSString *)appNameAndVersionNumberDisplayString {    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];    NSString *appDisplayName = [infoDictionary objectForKey:@"CFBundleDisplayName"];    NSString *majorVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];    NSString *minorVersion = [infoDictionary objectForKey:@"CFBundleVersion"];    return [NSString stringWithFormat:@"%@, Version %@ (%@)",                 appDisplayName, majorVersion, minorVersion];}

Now revving a minor version manually can be a pain, and so using a source repository revision number trick is ideal. If you've not tied that in (as I hadn't), the above snippet can be useful. It also pulls out the app's display name.


Swift version for both separately:

Swift 3

let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! Stringlet buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String

Swift 2

let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! Stringlet buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String

Its included in this repo, check it out:

https://github.com/goktugyil/EZSwiftExtensions