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?

Share:
916
AWhitford
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, 2022

Comments

  • AWhitford
    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
      Joshua over 4 years
      Hmm, 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
      AWhitford over 4 years
      What 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
    AWhitford over 4 years
    The 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
    AWhitford about 4 years
    Heads 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.