getIosVersion static method

Map<String, String?>? getIosVersion()

Reads and returns the version from the iOS Info.plist file. @returns {Map<String, String?>?} A map with 'versionName' and 'buildNumber', or null if the file is not found/readable, or an empty map if parsing fails.

Implementation

static Map<String, String?>? getIosVersion() {
  final plistFilePath = p.join(
    Directory.current.path,
    'ios',
    'Runner',
    'Info.plist',
  );
  final plistFile = File(plistFilePath);

  if (!plistFile.existsSync()) {
    return null; // File not found
  }

  try {
    final content = plistFile.readAsStringSync();
    final versionNameMatch = RegExp(
      r'<key>CFBundleShortVersionString</key>\s*<string>([^<]+)</string>',
    ).firstMatch(content);
    final buildNumberMatch = RegExp(
      r'<key>CFBundleVersion</key>\s*<string>([^<]+)</string>',
    ).firstMatch(content);

    if (versionNameMatch != null && buildNumberMatch != null) {
      return {
        'versionName': versionNameMatch.group(1),
        'buildNumber': buildNumberMatch.group(1),
      };
    }
    return {}; // File found but version not parsed
  } on FileSystemException {
    return null; // Error reading file
  }
}