0

I am trying to get my app version, after following this I tried this code :

import foundation
var config: [String: Any]?

if let infoPlistPath = Bundle.main.url(forResource: "Info", withExtension: "plist") {
    do {
        let infoPlistData = try Data(contentsOf: infoPlistPath)
        
        if let dict = try PropertyListSerialization.propertyList(from: infoPlistData, options: [], format: nil) as? [String: Any] {
            config = dict
        }
    } catch {
        print(error)
    }
}

print(config?["CFBundleName"])
// Optional(example-info)

print(config?["CFBundleVersion"])
// Optional(1)

print(config?["CFBundleShortVersionString"])
// Optional(1.0)

but I get always nil as a result.

Does that mean that my app has no version? If so how to set a version to it?

In addition, when I investigate the Info.plist file with Xcode I found that it's empty!

RTXGamer
  • 3,215
  • 6
  • 20
  • 29
A.HADDAD
  • 1,809
  • 4
  • 26
  • 51
  • 1
    Are you building an .app bundle, or a command line tool? Did you specify the `Info.plist` file (Xcode does this by default when creating new app). – Cristik Sep 09 '21 at 14:56
  • I am building a command line tool – A.HADDAD Sep 09 '21 at 14:57
  • That's the cause of your problem, command line apps consist just the executable, while app bundles (.app) are actually directories containing the executable, and the Info.plist file (among others). – Cristik Sep 09 '21 at 14:59
  • 2
    See this: https://stackoverflow.com/q/55518922/1187415 and this: https://stackoverflow.com/q/7797773/1187415 about how to embed an Info.plist in a command line tool. – Martin R Sep 09 '21 at 15:00
  • post your real code `import foundation` ??? – Leo Dabus Sep 09 '21 at 15:09

1 Answers1

0

Thanks to @Martin R is comment i solved my problem by linking a new plist file to the project

A.HADDAD
  • 1,809
  • 4
  • 26
  • 51