Skip to content

Instantly share code, notes, and snippets.

@NathanWalker
Last active May 6, 2025 20:27
Show Gist options
  • Save NathanWalker/3abd41d2205b4e78f5a620f4532b7c45 to your computer and use it in GitHub Desktop.
Save NathanWalker/3abd41d2205b4e78f5a620f4532b7c45 to your computer and use it in GitHub Desktop.
NativeScript method for getting app version and build info anytime. Works for iOS, Android and even Vision Pro + Meta Quest.
import { Utils } from '@nativescript/core'
export function getAppVersionInfo() {
let versionName: string;
let buildNumber: string;
if (__APPLE__) {
versionName = NSBundle.mainBundle.objectForInfoDictionaryKey('CFBundleShortVersionString');
buildNumber = NSBundle.mainBundle.objectForInfoDictionaryKey('CFBundleVersion');
} else {
const pi = Utils.android
.getApplicationContext()
.getPackageManager()
.getPackageInfo(Utils.android.getApplicationContext().getPackageName(), 0);
versionName = pi.versionName;
buildNumber = pi.versionCode.toString();
}
return {
versionName,
buildNumber,
versionDisplay: `${versionName} (${buildNumber})`
};
}
// {
// versionName: '1.0.0',
// buildNumber: '2025',
// versionDisplay: '1.0.0 (2025)'
// }
@NathanWalker
Copy link
Author

If you're using something like Sentry and dynamically insert version/build info during CI, you can use process.env vars, for example:

export function getAppVersionInfo() {
  let versionName: string;
  let buildNumber: string;
  if (__APPLE__) {
    const infoPlistVersion = NSBundle.mainBundle.objectForInfoDictionaryKey('CFBundleShortVersionString');
    try {
      versionName = process.env.APP_VERSION_STRING || infoPlistVersion;
    } catch (err) {
      versionName = infoPlistVersion;
    }
    const infoPlistBuild = NSBundle.mainBundle.objectForInfoDictionaryKey('CFBundleVersion');
    try {
      buildNumber = process.env.APP_BUILD_NUMBER || infoPlistBuild;
    } catch (err) {
      buildNumber = infoPlistBuild;
    }
  } else {
    const pi = Utils.android
      .getApplicationContext()
      .getPackageManager()
      .getPackageInfo(Utils.android.getApplicationContext().getPackageName(), 0);
    try {
      versionName = process.env.APP_VERSION_STRING || pi.versionName;
    } catch (err) {
      versionName = pi.versionName;
    }
    try {
      buildNumber = process.env.APP_BUILD_NUMBER || pi.versionCode.toString();
    } catch (err) {
      buildNumber = pi.versionCode.toString();
    }
  }
  return {
    versionName,
    buildNumber,
    versionDisplay: `${versionName} (${buildNumber})`
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment