开发者

Using CFBundleGetVersionNumber

I want to get the version number from info.plist using:

NSString *version = (NSString *)CFBundleGetVersionNumber(CFBundleGetMainBundle());

That doesn't give me the string value from Bundle Version that I expect.

This does give me the string I expect:

NSString *version = (NSString *)CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);

I'd like to know how to get the first form to work since it looks like it's the preferred way to get the version from info.plist.

Here is how I'm using the result:

htmlAbout = [htmlAbout stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"[version]"] wit开发者_如何学PythonhString:version];

I'm working in Xcode 4.1

Thanks


a serious error in conversion exists in your program: CFBundleGetVersionNumber returns UInt32, which is not convertible via typecast to NSString (or any objc type).


CFStringRef ver = CFBundleGetValueForInfoDictionaryKey(
                      CFBundleGetMainBundle(), 
                      kCFBundleVersionKey);
NSString *appVersion = (NSString *)ver;

If you are using ARC ,

CFStringRef ver = CFBundleGetValueForInfoDictionaryKey(
                      CFBundleGetMainBundle(), 
                      kCFBundleVersionKey);
NSString *appVersion = (__bridge NSString *)ver;


See the Apple docs for CFBundleGetVersionNumber:

https://developer.apple.com/library/mac/#documentation/CoreFOundation/Reference/CFBundleRef/Reference/reference.html

It says your second form is actually the preferred way for X.Y versions:

Where other version number styles—namely X, or X.Y, or X.Y.Z—are used, you can use CFBundleGetValueForInfoDictionaryKey with the key kCFBundleVersionKey to extract the version number as a string from the bundle’s information dictionary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜