How to Get Your App’s Display Name and Version

Dec 13, 2009   //   by Derek van Vliet   //   Development  //  3 Comments

Have you ever needed to retrieve your iPhone app’s name or version at runtime? Sure, you can use constants in a lot of cases, but it can often save you time in the long run to make use of the information that is available via a class called NSBundle.

The Code

You can retrieve your iPhone app’s display name using the following one line of code:

NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];

To retrieve your iPhone app’s version number, use the following similar line of code:

NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

Use Case

Screen shot 2009-12-13 at 12.41.07 PMIn Addicus, when you click the “Get Set” button on the main menu, it links to our website and passes in the referring application name and app version as querystring parameters, allowing us to track visits to our website from different versions of all of our iPhone games. To do this, we combine the above code with code that opens a URL. Here is the resulting code:

NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *url = [NSString stringWithFormat:@"http://getsetgames.com/?r=%@&v=%@",appName,appVersion];
 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

Since we’re retrieving this info via the NSBundle class, we never need to change this code when we update the version of the app. The version change propagates down to the URL we open automatically. And this code can be reused in any app without changing it.

3 Comments

  • appName works.
    But for appVersion… I get nothing.

    What should my app's pList look like?

  • Hey Susan,

    You shouldn't have to change anything in your app's info.plist to make this work. Here's what a typical info.plist would look like:

    <?xml version=”1.0″ encoding=”UTF-8″?>
    <!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
    <plist version=”1.0″>
    <dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleDisplayName</key>
    <string>My App Name</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleIconFile</key>
    <string>Icon.png</string>
    <key>CFBundleIdentifier</key>
    <string>com.xxxxxxxx.xxxxxx</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>MCApplicationId</key>
    <string>xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</string>
    <key>UIPrerenderedIcon</key>
    <true/>
    <key>UIStatusBarHidden</key>
    <true/>
    </dict>
    </plist>

  • Hey Susan,

    You shouldn't have to change anything in your app's info.plist to make this work. Here's what a typical info.plist would look like:

    <?xml version=”1.0″ encoding=”UTF-8″?>
    <!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
    <plist version=”1.0″>
    <dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleDisplayName</key>
    <string>My App Name</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleIconFile</key>
    <string>Icon.png</string>
    <key>CFBundleIdentifier</key>
    <string>com.xxxxxxxx.xxxxxx</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>MCApplicationId</key>
    <string>xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</string>
    <key>UIPrerenderedIcon</key>
    <true/>
    <key>UIStatusBarHidden</key>
    <true/>
    </dict>
    </plist>

Leave a comment

Our Games

Latest Tweets