Created
September 7, 2018 01:33
-
-
Save mannd/d5188991deaae17f330af8877e5f68e1 to your computer and use it in GitHub Desktop.
iOS getVersion() Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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