renameIOS static method

Future<void> renameIOS(
  1. String filePath,
  2. String appName
)

Implementation

static Future<void> renameIOS(String filePath, String appName) async {
  String data = await File(filePath).readAsString();
  XmlDocument document = XmlDocument.parse(data);
  var keys = document
      .findElements('plist')
      .first
      .findElements('dict')
      .first
      .children;

  /// Removing xml elements which are generated due to line breaks (this xml parser is creating xml element as 'XmlText' for line breaks)
  keys.removeWhere((element) => element is XmlText);
  var foundDisplayName = false;
  for (int i = 0; i < keys.length; i++) {
    /// Only the user facing name (CFBundleDisplayName) is changed.
    /// CFBundleName is the short/internal bundle name, so it is left as is.
    if (keys[i].innerText == 'CFBundleDisplayName') {
      foundDisplayName = true;
      var value = XmlElement(XmlName('string'));
      value.innerText = appName;
      keys.removeAt(i + 1);
      keys.insert(i + 1, value);
    }
  }

  /// Some projects have no CFBundleDisplayName at all. Add it so the rename
  /// still takes effect now that CFBundleName is no longer touched.
  if (!foundDisplayName) {
    var key = XmlElement(XmlName('key'));
    key.innerText = 'CFBundleDisplayName';
    var value = XmlElement(XmlName('string'));
    value.innerText = appName;
    keys.add(key);
    keys.add(value);
  }
  await saveFile(filePath, document.toXmlString(pretty: true));
  printFinishMessage('ios');
}