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?

1

2 Answers 2

8

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).

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.