Skip to content

Commit

Permalink
refactor: Create secrets.xml and Secrets.plist files for API keys
Browse files Browse the repository at this point in the history
  • Loading branch information
marianhlavac committed Sep 7, 2022
1 parent 7654e38 commit 2dc649d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
android:requestLegacyExternalStorage="true">
<meta-data android:name="flutterEmbedding"
android:value="2" />
<meta-data android:name="com.google.android.geo.API_KEY"
android:value=""/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_api_key"
/>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
Expand Down
8 changes: 8 additions & 0 deletions android/app/src/main/res/values/.secrets.example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Google Maps API key -->
<!-- https://developers.google.com/maps/documentation/maps-static/get-api-key -->
<string name="google_maps_api_key" translatable="false" templateMergeStrategy="preserve">
<!-- AIzaSyB... -->
</string>
</resources>
4 changes: 4 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
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 = "<group>"; };
5688DF4728C8C3730071F7AD /* Secrets.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Secrets.plist; sourceTree = "<group>"; };
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -112,6 +114,7 @@
97C146F01CF9000F007C117D /* Runner */ = {
isa = PBXGroup;
children = (
5688DF4728C8C3730071F7AD /* Secrets.plist */,
97C146FA1CF9000F007C117D /* Main.storyboard */,
97C146FD1CF9000F007C117D /* Assets.xcassets */,
97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -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 */,
Expand Down
15 changes: 0 additions & 15 deletions ios/Runner/AppDelegate.example.swift

This file was deleted.

27 changes: 27 additions & 0 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
9 changes: 9 additions & 0 deletions ios/Runner/Secrets.example.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- https://developers.google.com/maps/documentation/maps-static/get-api-key -->
<key>GoogleMapsAPIKey</key>
<string><!-- AIzaSyB... --></string>
</dict>
</plist>

0 comments on commit 2dc649d

Please sign in to comment.