Swift version: 5.6
All iOS apps must store an app version number in their Info.plist file, but there’s no build-in way to get that as a string you can use in your code.
Fortunately, we can write a small extension to UIApplication
that reads the Info.plist file and returns a version number automatically. This might not exist for some reason, so we’re going to make a static variable that returns an optional string in case there was a problem reading it.
Here’s the code:
extension UIApplication {
static var appVersion: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
}
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Available from iOS 8.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.