3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

環境ごとにFirebaseプロジェクトを使い分ける方法

Posted at

初めに

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)
    }

参考

FirebaseDocument/複数のプロジェクトを構成する

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?