Skip to content

Instantly share code, notes, and snippets.

@mannd
Created September 7, 2018 01:33
Show Gist options
  • Save mannd/d5188991deaae17f330af8877e5f68e1 to your computer and use it in GitHub Desktop.
Save mannd/d5188991deaae17f330af8877e5f68e1 to your computer and use it in GitHub Desktop.
iOS getVersion() Swift
func getVersion() -> String {
// if this doesn't unwrap, something is really wrong
guard let dictionary = Bundle.main.infoDictionary else {
preconditionFailure("Error: main bundle not found!")
}
let version = dictionary["CFBundleShortVersionString"] ?? "error"
#if DEBUG // Get build number, if you want it. Cleaner to leave out of release version.
let build = dictionary["CFBundleVersion"] ?? "error"
// the version+build format is recommended by https://semver.org
let versionBuild = "\(version)+\(build)"
return versionBuild
#else
return version
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment