初めに
Firebaseを利用して開発を行う際に、開発環境と本番環境とでそれぞれ接続するFirebaseプロジェクトを変えて開発したい際にどうやって実現するかを書いてみました。
実現方法のアプローチ
・Schemeごと(開発、本番)に異なるBuildConfigurationを設定。
・設定したBuild Configurationを元に読み込むGoogleService.plistを決定。
Schemeごと(開発、本番)に異なるBuildConfigurationを設定
設定したBuildConfigurationを元に読み込むGoogleService.plistを決定
環境に対するGoogleService.plistの命名
prodとDevのGoogleService.plistの命名は例として以下の内容になります。
環境 | GoogleService.plist |
---|---|
Prod | GoogleService-prod.plist |
Dev | GoogleService-dev.plist |
Schemeに設定したBuildConfigurationで使用するGoogleService.plistを決める
Config.swift
import Foundation
import UIKit
struct Config {
private init() {}
struct Firebase {
static func resourceFilePath() -> String {
#if Prod
return Bundle.main.path(forResource: "GoogleService-prod.plist", ofType: "plist")!
#else
return Bundle.main.path(forResource: "GoogleService-dev.plist", ofType: "plist")!
#endif
}
}
}
FirebaseApp.configure()で環境に合わせて設定
AppDelegate.swift
import UIKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let options: FirebaseOptions = FirebaseOptions(contentsOfFile: Config.Firebase.resourceFilePath())!
FirebaseApp.configure(options: options)
}