6 June 2024 Leave a comment Tech-Help
If you’re developing an iOS app and need to log events such as user logins or track which versions of your app users are running, knowing how to retrieve the app version and build number programmatically can be very useful. In this guide, we’ll walk you through the process using Swift.
Retrieving App Version and Build Number
Here’s a straightforward way to get the app version and build number using Swift. This method works for Swift 4.2 and newer versions:
extension Bundle {
var releaseVersionNumber: String? {
return infoDictionary?["CFBundleShortVersionString"] as? String
}
var buildVersionNumber: String? {
return infoDictionary?["CFBundleVersion"] as? String
}
}
let appVersion = Bundle.main.releaseVersionNumber
let appBuild = Bundle.main.buildVersionNumber
In the above code, we define an extension for the Bundle
class to add properties for retrieving the version and build numbers from the app’s info dictionary. The CFBundleShortVersionString
key provides the app’s version number, while the CFBundleVersion
key gives the build number.
Practical Example
Let’s see how you might use this in a practical scenario, such as displaying the version and build number in a label:
if let version = Bundle.main.releaseVersionNumber, let build = Bundle.main.buildVersionNumber {
versionLabel.text = "Version: \(version) (Build \(build))"
}
In this example, we check if the version and build numbers are available and then set the text of a label to display them.
Using the Information in Your App
You might want to use the version and build information for various purposes, such as logging events, showing them in the UI, or sending them to an analytics server. Here’s an example of logging the version and build number:
func logAppVersion() {
if let version = Bundle.main.releaseVersionNumber, let build = Bundle.main.buildVersionNumber {
print("App Version: \(version), Build: \(build)")
}
}
logAppVersion()
This function prints the app’s version and build number to the console, which can be useful for debugging or logging purposes.
Enhancing Your Workflow with Repeato
Managing app versions and builds is crucial for maintaining a robust development workflow. However, ensuring that your app functions correctly across different versions can be challenging. This is where automated testing tools like Repeato can make a significant difference.
Repeato is a no-code test automation tool for iOS and Android that uses computer vision and AI to create, run, and maintain automated tests quickly. By automating repetitive testing tasks, Repeato allows developers to focus on creating a great product rather than spending time on test maintenance. It also enables non-technical team members to handle test automation, ensuring that your app works flawlessly across different versions and builds.
For more information on how Repeato can streamline your testing process, visit our documentation or check out our blog for the latest updates.