How can my iphone app detect its own version number?

Viewed 113733

I'm writing an iPhone app. It's already been published, but I would like to add a feature where its version number is displayed.

I'd rather not have to do this manually with each version I release...

Is there a way in objective-C to find out what the version is of my app?

16 Answers

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)".

You can specify the CFBundleShortVersionString string in your plist.info and read that programmatically using the provided API.

Swift 5:

There are two things - App version and build version

  • To get App version:

     if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
          // present appVersion
    }
    
  • To get Build version:

     if let buildVersion = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
          // present buildVersion
     }
    

Thanks @Brad Larson♦ a lot

Read the info.plist file of your app and get the value for key CFBundleShortVersionString. Reading info.plist will give you an NSDictionary object

func getAppVersion() -> String {
    let dictionary = Bundle.main.infoDictionary!
    let versionValue = dictionary["CFBundleShortVersionString"] ?? "0"
    let buildValue = dictionary["CFBundleVersion"] ?? "0"
    return "\(versionValue) (build \(buildValue))"
}

Based on @rajat chauhan answer without forced cast to String.

This is a good thing to handle with a revision control system. That way when you get a bug report from a user, you can check out that revision of code and (hopefully) reproduce the bug running the exact same code as the user.

The idea is that every time you do a build, you will run a script that gets the current revision number of your code and updates a file within your project (usually with some form of token replacement). You can then write an error handling routine that always includes the revision number in the error output, or you can display it on an "About" page.

You can try this method:

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
Related