2

I want to programmatically access the version and build information that is contained in MyFramework.framework at runtime.

I found some solutions here but they don't work. After translating to Swift 3, I find that Bundle.main.infoDictionary is empty.

How can I get this information?

Community
  • 1
  • 1
Raphael
  • 9,779
  • 5
  • 63
  • 94
  • 1
    Related: [Loading a resource (e.g. storyboard) in a Swift framework](http://stackoverflow.com/questions/24982848/loading-a-resource-e-g-storyboard-in-a-swift-framework). – Martin R May 21 '17 at 20:09

2 Answers2

6

It seems that the "main" bundle is reserved for app(lication)s. Loading a bundle for a framework class, that is

Bundle(for: MyClass.self)

works; its infoDictionary contains the expected values for keys "CFBundleShortVersionString" (version) and "CFBundleVersion" (build).

Raphael
  • 9,779
  • 5
  • 63
  • 94
  • To do: verify that the information is still there and correct if the code is run from an application context, not from within tests for the the framework on its own. – Raphael May 21 '17 at 19:31
0

Don't use Bundle(for: MyClass.self), I experienced a bug, you might get application’s version number.

ChatGPT:

If there are multiple classes named MyClass (for instance, one in your app and one in your framework), then the outcome would depend on which MyClass class is being referenced at runtime.

I fixed it by just hard coding version number. Use below code instead (Suggested by Bing Chat and Bard) or just hard code version number.

    let frameworkBundle = Bundle(identifier: "com.example.framework")
    let frameworkVersion = frameworkBundle?.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
    // or
    let frameworkVersion = "1.0"
Steve Ham
  • 3,067
  • 1
  • 29
  • 36