How do I access the Info.plist values from Swift in Xcode 12

Viewed 2897

I would like to be able to access the versioning values in Info.plist from within my iOS app, writing in Swift in Xcode 12.5. The key/values in question are:

CFBundleShortVersionString: $(MARKETING_VERSION)
CFBundleVersion: $(CURRENT_PROJECT_VERSION)

From the Build Settings, the two Info.plist values contain "1.0" and "8" respectively. I've tried the following:

let appBundleVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String

which works if I have user-defined key/value pairs in the Info.plist, but doesn't work for accessing the system-managed Build Settings. How can I access these two values?

Thanks.

1 Answers

The answer was implied in my question. I defined two user-defined key/value pairs in the Info.plist file:

App_version_number: $(MARKETING_VERSION)
App_build_number: $(CURRENT_PROJECT_VERSION)

and referenced these in the code:

let appBuildNumber = Bundle.main.object(forInfoDictionaryKey: "App_build_number") as? String
let appVersionNumber = Bundle.main.object(forInfoDictionaryKey: "App_version_number") as? String

Perhaps there's a direct way to access the Build Settings, but this indirect method worked fine.

Related