addGoogleMaps method
Adds the GoogleMaps import and GMSServices.provideAPIKey call with
apiKey to AppDelegate.swift.
Implementation
List<String> addGoogleMaps(String apiKey) {
final results = <String>[];
try {
final file = File(_delegatePath);
var content = file.readAsStringSync();
var modified = false;
// 1. Add import GoogleMaps
if (!content.contains('import GoogleMaps')) {
final marker = _annotationMarker(content);
if (marker != null) {
final idx = content.indexOf(marker);
content =
content.substring(0, idx) + 'import GoogleMaps\n\n' + content.substring(idx);
modified = true;
results.add('✅ Updated AppDelegate.swift — added import GoogleMaps');
}
} else {
results.add(
'⚠️ AppDelegate.swift — import GoogleMaps already exists, skipped');
}
// 2. Add GMSServices.provideAPIKey as first line of didFinishLaunching
if (!content.contains('GMSServices.provideAPIKey(')) {
final bodyStart = _didFinishLaunchingBodyStart(content);
if (bodyStart != -1) {
final newlineAfterBrace = content.indexOf('\n', bodyStart);
if (newlineAfterBrace != -1) {
content = content.substring(0, newlineAfterBrace + 1) +
' GMSServices.provideAPIKey("$apiKey")\n' +
content.substring(newlineAfterBrace + 1);
modified = true;
results.add(
'✅ Updated AppDelegate.swift — added GMSServices.provideAPIKey');
}
}
} else {
results.add(
'⚠️ AppDelegate.swift — GMSServices.provideAPIKey already exists, skipped');
}
if (modified) file.writeAsStringSync(content);
} catch (e) {
results.add('❌ Failed to update AppDelegate.swift: $e\n'
'Please open an issue: https://github.com/MohsenBahaj/flutter_ios_capabilities_setup/issues');
}
return results;
}