天天看点

How to Get App Name and Version

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.

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

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.

- See more at: http://getsetgames.com/2009/12/13/iphonedev-advent-tip-13-how-to-get-your-apps-display-name-and-version/#sthash.4yLKz7q9.dpuf