Get version number of iOS Universal Framework in client

只谈情不闲聊 提交于 2019-11-27 02:44:10

问题


This is likely not limited to iOS Universal Frameworks but all xxx.framework files. However I can't seem to find documentation on how to get the current version and build of a framework within the client application. Within an app you'd use something like:

    NSString *name = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

That would give you the current information stored in the Info.plist for the app. But how to we find that information for a framework. And in my case, specifically an embedded framework.


回答1:


I have found that Apple's new Cocoa Touch frameworks supported in Xcode 6, offer an easy answer to this problem. In the default header file created for you, something like Framework.h, you'll see two constants declared for you. These are defined later presumably at runtime by internal framework logic. But I have confirmed they're pulled from the plist for the framework.

//! Project version number for LocalSearch.
FOUNDATION_EXPORT double FrameworkVersionNumber;

//! Project version string for LocalSearch.
FOUNDATION_EXPORT const unsigned char FrameworkVersionString[];



回答2:


Here's a solution that does work with Universal Frameworks. Just replace SomeFrameworkClass with a class from the desired framework.

if let sdkVersion = Bundle(for: SomeFrameworkClass.self).infoDictionary?["CFBundleShortVersionString"] {
    // sdkVersion is available here
}



回答3:


Let's say Foo is a class from the given framework, you can use something like :

NSDictionary *infoDictionary = [[NSBundle bundleForClass: [Foo class]] infoDictionary]; 

NSString *name = [infoDictionary valueForKey:(__bridge NSString*)kCFBundleNameKey];
NSString *version = [infoDictionary valueForKey:(__bridge NSString*)kCFBundleVersionKey];

NSLog(@"%@ version %@", name, version);

In Swift :

// `Foo` is a type defined in the framework
if  let infos = Bundle(for: Foo.self).infoDictionary,
    let name = infos[kCFBundleNameKey as String],
    let version = infos[kCFBundleVersionKey as String] {     
        print("Using \(name) version \(version)")

        if let shortVersion = infos["CFBundleShortVersionString"] as? String {
            print("Short version : " + shortVersion)
        }
    }

Note : For some reason, "CFBundleShortVersionString" is not defined in a constant, cf Is there a constant defined for CFBundleShortVersionString in iOS/MacOS framework




回答4:


This is for who may want to have a function on your framework that returns or print on the console it's version with build number. Just make sure you are using the class you are sharing on your Framework.h when declaring the *infoDictionary. In my example I'm using a class named cloud

+(NSString *)frameWorkVersion {
    NSDictionary *infoDictionary = [[NSBundle bundleForClass: [cloud class]] infoDictionary];
    NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *build = [infoDictionary objectForKey:(NSString *)kCFBundleVersionKey];
    NSString *fVersion = [NSString stringWithFormat:@"%@.%@",version,build];
    NSLog(@"Framework Version %@",fVersion);

    return fVersion; }


来源:https://stackoverflow.com/questions/24538215/get-version-number-of-ios-universal-framework-in-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!