How to use Firebase Remote Config in AppDelegate.swift?
916
Based on this article, I came up with:
import UIKit
import Firebase
import Flutter
import GoogleMaps
//import os.log
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
RemoteConfig.remoteConfig().fetchAndActivate() { status, error in
let apiKey : String = RemoteConfig.remoteConfig()["Google_Maps_SDK_for_iOS_API_KEY"].stringValue ?? "MISSING";
// os_log("Google_Maps_SDK_for_iOS_API_KEY = '%@'", apiKey)
GMSServices.provideAPIKey(apiKey)
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Is there a better way?
Author by
AWhitford
Polyglot software engineer that likes to build solutions. Really enjoying Flutter, Dart, and Firebase recently. Scala is my most admired language and has been highly influential. TypeScript feels better than JavaScript. Docker is nice, but challenge you to think serverless. DevOps is a lifestyle choice.
Updated on December 18, 2022Comments
-
AWhitford over 1 year
Based on advice, I would like to manage API keys using Firebase Remote Config to avoid hard-coding API keys like google_maps_flutter suggests. It has an
AppDelegate.swift
like:import UIKit import Flutter import GoogleMaps @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GMSServices.provideAPIKey("YOUR KEY HERE") GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
How can the above be modified to fetch the API key from Firebase Remote Config and then pass it to
GMSServices
?-
Joshua over 4 yearsHmm, does this mean you are willing to wait for remote config to be fetch before you actually register your key? and if it fails fetching you are fine in not having that functionality. if so then remoteConfig has an option called fetch with a callback block
-
AWhitford over 4 yearsWhat is the alternative? I'm just trying to avoid hard-coding API keys in source code. I'm seeking best practice advice for production: github.com/flutter/flutter/issues/51267
-
-
AWhitford over 4 yearsThe biggest problem seems to be the fact that the API key can only be passed via SWIFT code, not Dart code. If the Dart API was extended, then I can easily move the API key management to the Flutter/Dart app and provide excellent error handling. As a result, I proposed an API enhancement: github.com/flutter/flutter/issues/51433
-
AWhitford about 4 yearsHeads up! I discovered that the above code interferes with the very first launch of an app that uses
firebase_remote_config
. See github.com/FirebaseExtended/flutterfire/issues/2592 for details.