- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I get the version and build number of an application using Swift?
You often need to deal with the version and build number of the iOS application. It helps you to perform debugging operations on the server side. You can check which version or build the client (IOS app user) has. Based on that, you can debug the issues coming from the client side.
Version and build numbers might be needed to display to the user to verify what version and build are currently running by the end user.
To get the app version and build number in Swift, you can use the Bundle class, which provides access to the app's bundle information.
Here is an example of how you can get the app version and build number using Swift −
Syntax
Note that the object(forInfoDictionaryKey:) method returns an optional, so you will need to use optional binding or force unwrapping to get the values. In the example above, I have used force unwrapping, which means that the code will crash if the values are not present in the app's info dictionary. You may want to use optional binding to avoid this.
let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String
Basically, we fetch the build number from the info.plist file which is stored in the main bundle in the application. There is a predefined key CFBundleVersion to access the build version.
Algorithm
Step 1 − Access the main bundle object
Step 2 − Access the info dictionary object from the main bundle object
Step 3 − Access the build version from the CFBundleShortVersionString key
Example
Assuming that you are running the below code on an actual project on Xcode −
import Foundation if let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String { print("App version: \(appVersion)") } else { print("your platform does not support this feature.") } if let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String { print("Build number: \(buildNumber)") } else { print("your platform does not support this feature.") }
Output
your platform does not support this feature. your platform does not support this feature.
The output may vary according to the Xcode project currently running on your machine.
This will print the app version and build number to the console. The app version is typically a string in the form of "X.X.X" (e.g. "1.0.0"), while the build number is a string that represents the build number of the app (e.g. "1").
Example
Assuming that you are running the following code on an online compiler −
import Foundation if let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String { print("App version: \(appVersion)") } else { print("your platform does not support this feature.") } if let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String { print("Build number: \(buildNumber)") } else { print("your platform does not support this feature.") }
Output
your platform does not support this feature. your platform does not support this feature.
Conclusion
In Swift, many times you need to play with the build and version to perform some action. It is an easy process to access the current version of the application used in the device.