iOS App, Programmatically Get Build Version

iOS app, programmatically get build version

The value you set in the Xcode target summary's "Version" field is in here:

Swift 3

let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String

ObjC

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

Swift 2

let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String

and the "Build":

Swift 3

let build = Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String

ObjC

NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];

Swift 2

let build = NSBundle.mainBundle().infoDictionary?[kCFBundleVersionKey as String] as! String

How to programmatically display version/build number of target in iOS app?

There are 2 Numbers!

The marketing release number is for the customers, called version number. It starts with 1.0 and goes up for major updates to 2.0, 3.0, for minor updates to 1.1, 1.2 and for bug fixes to 1.0.1, 1.0.2 . This number is oriented about releases and new features. It does not have to stop at 9, 1.11.23 is a reasonable version number.

The build number is mostly the internal number of builds that have been made until then. But some use other numbers like the branch number of the repository or its commit number. This number should be unique to distinguish the different builds, which only have minor incremental changes.


To get the version number:

Objective-C:

NSString * appVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

Swift < 3.0:

let appVersionString: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String

Swift 3.0+ (tested with 5.0):

let appVersionString: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String

To get the build number:

Objective-C:

NSString * appBuildString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

Swift < 3.0:

let buildNumber: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String

Swift 3.0+ (tested until 5.0):

let buildNumber: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String   

If you want both in one:

First use the above lines and then the following one.

Objective-C:

NSString * versionBuildString = [NSString stringWithFormat:@"Version: %@ (%@)", appVersionString, appBuildString];

Swift (tested until 5.0):

let versionAndBuildNumber: String = "\(appVersionString) (\(buildNumber))"

Notes:

The values in the main bundle are not always present, for example in a command line application there is no CFBundleShortVersionString or CFBundleVersion, so the methods will return nil and it will crash because in the code it makes a incorrect downcast.
But in normal Cocoa iOS and Mac apps these values are defined and will not be deleted.

This is tested with Xcode Version 7.3 (7D175). The build number is often written in parenthesis / braces. The build number is in hexadecimal or decimal.

buildandversion


In Xcode you can auto-increment the build number as a decimal number by placing the following in the Run script build phase in the project settings

#!/bin/bash    
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

For hexadecimal build number use this script

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$((0x$buildNumber))
buildNumber=$(($buildNumber + 1))
buildNumber=$(printf "%X" $buildNumber)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

For Xcode do the following:

Step 1

step1

Step 2

step2

Step 3

step3

How do I get the App version and build number using Swift?

EDIT

Updated for Swift 4.2

let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String

EDIT

As pointed out by @azdev on the new version of Xcode you will get a compile error for trying my previous solution, to solve this just edit it as suggested to unwrap the bundle dictionary using a !

let nsObject: AnyObject? = Bundle.main.infoDictionary!["CFBundleShortVersionString"]

End Edit

Just use the same logic than in Objective-C but with some small changes

//First get the nsObject by defining as an optional anyObject
let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary["CFBundleShortVersionString"]

//Then just cast the object as a String, but be careful, you may want to double check for nil
let version = nsObject as! String

How do I get the current version of my iOS project in code?

You can get the version and build numbers as follows:

let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String

or in Objective-C

NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];

I have the following methods in a category on UIApplication:

extension UIApplication {

static var appVersion: String {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
}

static var appBuild: String {
return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String
}

static var versionBuild: String {
let version = appVersion, build = appBuild
return version == build ? "v\(version)" : "v\(version)(\(build))"
}
}

Gist: https://gist.github.com/ashleymills/6ec9fce6d7ec2a11af9b


Here's the equivalent in Objective-C:

+ (NSString *) appVersion
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
}

+ (NSString *) build
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];
}

+ (NSString *) versionBuild
{
NSString * version = [self appVersion];
NSString * build = [self build];

NSString * versionBuild = [NSString stringWithFormat: @"v%@", version];

if (![version isEqualToString: build]) {
versionBuild = [NSString stringWithFormat: @"%@(%@)", versionBuild, build];
}

return versionBuild;
}

Gist: https://gist.github.com/ashleymills/c37efb46c9dbef73d5dd

How to read current app version in Xcode 11 with script

You can use it like any other project variable:

sourceFilePath="$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"
versionNumber="$MARKETING_VERSION"
buildNumber="$CURRENT_PROJECT_VERSION"

sed -i .bak -e "/userLabel=\"APP_VERSION_LABEL\"/s/text=\"[^\"]*\"/text=\"v$versionNumber\"/" "$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"

How to get build and version number of Flutter app

You can use package_info_plus.

The versions are extracted from:

Android:

build.gradle, versionCode and versionName

iOS:

Info.plist, CFBundleVersion

Usage

Add the dependency

  1. Add this to your package's pubspec.yaml file:
dependencies:
package_info_plus: ^1.0.6

  1. Import the file into your dart file:
import 'package:package_info_plus/package_info_plus.dart';

  1. if your method is marked as async:
PackageInfo packageInfo = await PackageInfo.fromPlatform();

String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;

If you don't want to use await/async:

PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;
});


Related Topics



Leave a reply



Submit