【Swift3】 Firebaseの設定ファイルをdebugビルドとreleaseビルドで切り替える
はじめに
Firebaseを使うには設定ファイル(plist)をプロジェクトに追加する必要があります。
debug環境もrelease環境も同じ設定ファイルを使う場合は特に問題がないのですが、debugビルドのみbundleIDを変えている場合にはFirebaseのコンソールで2つのアプリを登録し、それぞれ別々の設定ファイルGoogleService-Info.plistが生成されるのでビルド時に読み込むplistを切り替える必要があります。
GoogleService-Info.plistをビルドによって切り替える
まずはファイル名を変更します。ここではdebug用のファイルをGoogleService-Info-debug.plistとします。

次にAppDelegateでplistのファイル名の変数を#if DEBUGで切り替えるように定義します。
#if DEBUG let firebasePlistName = "GoogleService-Info-debug" #else let firebasePlistName = "GoogleService-Info" #endif
そして今まではFIRApp.configure()とconfigure()メソッドを呼んでいたところをオプションを指定するように修正します。
class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if let firbaseOptions = FIROptions(contentsOfFile: Bundle.main.path(forResource: firebasePlistName, ofType: "plist")) { FIRApp.configure(with: firbaseOptions) } return true }
もし3つ以上の環境を切り替える場合は定義する変数を増やして同じように切り替えるようにすればOKです。