From 2dc649d07c9a729475bdc4eb077fa1106e666bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=CC=81n=20Hlava=CC=81c=CC=8C?= Date: Wed, 7 Sep 2022 14:24:42 +0200 Subject: [PATCH] refactor: Create secrets.xml and Secrets.plist files for API keys --- .gitignore | 4 +-- README.md | 4 +-- ...nifest.example.xml => AndroidManifest.xml} | 7 ++--- .../src/main/res/values/.secrets.example.xml | 8 ++++++ ios/Runner.xcodeproj/project.pbxproj | 4 +++ ios/Runner/AppDelegate.example.swift | 15 ----------- ios/Runner/AppDelegate.swift | 27 +++++++++++++++++++ ios/Runner/Secrets.example.plist | 9 +++++++ 8 files changed, 56 insertions(+), 22 deletions(-) rename android/app/src/main/{AndroidManifest.example.xml => AndroidManifest.xml} (96%) create mode 100644 android/app/src/main/res/values/.secrets.example.xml delete mode 100644 ios/Runner/AppDelegate.example.swift create mode 100644 ios/Runner/AppDelegate.swift create mode 100644 ios/Runner/Secrets.example.plist diff --git a/.gitignore b/.gitignore index 432669a4..ab4f6187 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Files with google api key are ignored, so your key wont get accidentaly pushed to repository /assets/config/google_map_api.json -**/android/app/src/main/AndroidManifest.xml -**/ios/Runner/AppDelegate.swift +**/android/app/src/main/res/values/secrets.xml +**/ios/Runner/Secrets.plist # Miscellaneous *.class diff --git a/README.md b/README.md index 0cc75f56..e5bed7a6 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ The application uses [Google Maps Platform](https://cloud.google.com/maps-platfo For more details, see [Getting started with Google Maps Platform](https://developers.google.com/maps/gmp-get-started). -In the project folder, duplicate files *android/app/src/main/AndroidManifest.example.xml* and *ios/Runner/AppDelegate.example.swift*, remove the *.example* suffix. Paste your key to both files. Then create a file *google_map_api.json* in *assets/config*, see the example file to see the required structure. +In the project folder, create files *ios/Runner/Secrets.plist* and *android/app/res/values/secrets.xml* by making a copy of the example files present in the directories. Paste your key to both files. Then create a file *google_map_api.json* in *assets/config*, see the example file to see the required structure. -The files which contain Google Maps API are added to .gitignore, so your key will not be accidentally committed. +The files which contain Google Maps API are added to .gitignore, so your key will not be accidentally committed. Make sure you don't put your API keys anywhere else before pushing your work. > 💡 _This README is a work-in-progress and will be soon updated with all necessary information about the application architecture, ways to build the project, and ways to contribute to this project._ diff --git a/android/app/src/main/AndroidManifest.example.xml b/android/app/src/main/AndroidManifest.xml similarity index 96% rename from android/app/src/main/AndroidManifest.example.xml rename to android/app/src/main/AndroidManifest.xml index 9b4023a3..561ab9e0 100644 --- a/android/app/src/main/AndroidManifest.example.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -21,11 +21,12 @@ android:requestLegacyExternalStorage="true"> - + + + + + + + + \ No newline at end of file diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index 3eac8738..5b6284b3 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; }; 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; }; + 5688DF4828C8C3730071F7AD /* Secrets.plist in Resources */ = {isa = PBXBuildFile; fileRef = 5688DF4728C8C3730071F7AD /* Secrets.plist */; }; 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; }; 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; }; @@ -37,6 +38,7 @@ 2BC98C12242F60C5831A38C1 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = ""; }; 35BFAF0CF6BA8D03E05E4A22 /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; + 5688DF4728C8C3730071F7AD /* Secrets.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Secrets.plist; sourceTree = ""; }; 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = ""; }; 74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = ""; }; @@ -112,6 +114,7 @@ 97C146F01CF9000F007C117D /* Runner */ = { isa = PBXGroup; children = ( + 5688DF4728C8C3730071F7AD /* Secrets.plist */, 97C146FA1CF9000F007C117D /* Main.storyboard */, 97C146FD1CF9000F007C117D /* Assets.xcassets */, 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */, @@ -189,6 +192,7 @@ buildActionMask = 2147483647; files = ( 97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */, + 5688DF4828C8C3730071F7AD /* Secrets.plist in Resources */, 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */, 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */, 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */, diff --git a/ios/Runner/AppDelegate.example.swift b/ios/Runner/AppDelegate.example.swift deleted file mode 100644 index e7fd676f..00000000 --- a/ios/Runner/AppDelegate.example.swift +++ /dev/null @@ -1,15 +0,0 @@ -import UIKit -import Flutter -import GoogleMaps - -@UIApplicationMain -@objc class AppDelegate: FlutterAppDelegate { - override func application( - _ application: UIApplication, - didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? - ) -> Bool { - GMSServices.provideAPIKey("") - GeneratedPluginRegistrant.register(with: self) - return super.application(application, didFinishLaunchingWithOptions: launchOptions) - } -} diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift new file mode 100644 index 00000000..b8924180 --- /dev/null +++ b/ios/Runner/AppDelegate.swift @@ -0,0 +1,27 @@ +import UIKit +import Flutter +import GoogleMaps + +@UIApplicationMain +@objc class AppDelegate: FlutterAppDelegate { + override func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? + ) -> Bool { + guard let secretsFilePath = Bundle.main.path(forResource: "Secrets", ofType: "plist") else { + NSLog("Secrets.plist file was not found, make sure you've copied the Secrets.example.plist") + exit(1) + } + + let secrets = NSDictionary(contentsOfFile: secretsFilePath) + + guard let googleMapsKey = secrets?["GoogleMapsAPIKey"] else { + NSLog("There is no GoogleMapsAPIKey key in Secrets.plist") + exit(1) + } + + GMSServices.provideAPIKey(googleMapsKey as! String) + GeneratedPluginRegistrant.register(withRegistry: self) + return super.application(application, didFinishLaunchingWithOptions: launchOptions) + } +} diff --git a/ios/Runner/Secrets.example.plist b/ios/Runner/Secrets.example.plist new file mode 100644 index 00000000..702d39fc --- /dev/null +++ b/ios/Runner/Secrets.example.plist @@ -0,0 +1,9 @@ + + + + + + GoogleMapsAPIKey + + +