updateIosVersion static method

bool updateIosVersion(
  1. String versionName,
  2. String buildNumber
)

Updates the version in the iOS Info.plist file. @returns {bool} True if the file was updated, false otherwise.

Implementation

static bool updateIosVersion(String versionName, String buildNumber) {
  final plistFilePath = p.join(
    Directory.current.path,
    'ios',
    'Runner',
    'Info.plist',
  );
  final plistFile = File(plistFilePath);

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

  try {
    var content = plistFile.readAsStringSync();
    var originalContent = content;

    // Replace CFBundleShortVersionString
    final nameRegex = RegExp(
      r'(<key>CFBundleShortVersionString</key>\s*<string>)[^<]+(</string>)',
    );
    if (nameRegex.hasMatch(content)) {
      content = content.replaceFirst(nameRegex, '\$1$versionName\$2');
    }

    // Replace CFBundleVersion
    final codeRegex = RegExp(
      r'(<key>CFBundleVersion</key>\s*<string>)[^<]+(</string>)',
    );
    if (codeRegex.hasMatch(content)) {
      content = content.replaceFirst(codeRegex, '\$1$buildNumber\$2');
    }

    if (content != originalContent) {
      plistFile.writeAsStringSync(content);
      return true;
    }
    return false; // No changes made
  } on FileSystemException {
    return false; // Error reading/writing file
  }
}