firebase / firebase-ios-sdk

Firebase SDK for Apple App Development
https://firebase.google.com
Apache License 2.0
5.53k stars 1.44k forks source link

[FR]: Add FIROptions initializer to init it with options from a dictionary instead of a Plist #11726

Open jesus-mg-ios opened 1 year ago

jesus-mg-ios commented 1 year ago

Description

We want to change fir options to configure the firebase app, from a dictionary not from a plist.

API Proposal

FirOptions(_ map: [String: Any]), in case that this map is empty or does not include some keys, include them by default maybe (?)

Firebase Product(s)

AB Testing, Analytics, App Check, App Distribution, Authentication, Crashlytics, Database, DynamicLinks, Firestore, Functions, In-App Messaging, Installations, Messaging, Performance, Remote Config, Storage

google-oss-bot commented 1 year ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

paulb777 commented 1 year ago

See also #11190

peterfriese commented 12 months ago

This is possible today, here is an example:

  1. Create an extension on FirebaseOptions, and fill it with the respective configuration values from the GoogleService-Info.plist file for your project.
import Foundation
import FirebaseCore

extension FirebaseOptions {
  static var `default`: FirebaseOptions {
    let options = FirebaseOptions(googleAppID: "1:23xxxxxxxx",
                                  gcmSenderID: "23xxxxxxxx")
    options.clientID = "23xxxxxxxx.apps.googleusercontent.com"
    options.apiKey = "AIzxxxxxxx"
    options.projectID = "xxxxxxx"
    options.storageBucket = "xxxxxxxxx.appspot.com"
    options.databaseURL = "https://xxxxxxxxx.firebaseio.com"
    return options
  }
}
  1. In your AppDelegate or main entry point, call FirebaseApp.configure(options: FirebaseOptions.default) to initialise Firebase:
import SwiftUI
import FirebaseCore

class AppDelegate: NSObject, UIApplicationDelegate {
  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    FirebaseApp.configure(options: FirebaseOptions.default)
    return true
  }
}

@main
struct SofiaApp: App {
  // register app delegate for Firebase setup
  @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

  var body: some Scene {
    WindowGroup {
      RootView()
    }
  }
}

Also, as mentioned in https://github.com/firebase/firebase-ios-sdk/issues/11190#issuecomment-1523733099, keep in mind that obfuscating the plist file or putting the configuration into code doesn't increase the security of your app. To keep your users' data secure and prevent malicious actors from accessing your backend, you should write solid Security Rules and use App Check.

PS: you can also use this approach to use separate configurations for several environments:

  1. duplicate the file with the FirebaseOptions extension (say, create one named FirebaseOptionsProd.swift, and another one named FirebaseOptionsDev.swift)
  2. Duplicate your build target (one for prod, another one for dev)
  3. Make sure the FirebaseOptions<environment>.swift files are assigned to the respective build target.