LoginSignup
1
1

More than 3 years have passed since last update.

ビルド設定で読み込むfirebaseのinfo.plistを変える

Posted at

毎回どうするんだっけなと過去のプロジェクト開くのがめんどいから

MyFirebase.swift
import Firebase

//コード内にマクロ入れるとごちゃつくのでここで定数化しておく
#if DEBUG
let IS_DEBUG = true
#else
let IS_DEBUG = false
#endif

import UIKit
import Firebase

class MyAppFirebase:NSObject{
  static let shared = MyAppFirebase()
  static var FIREBASE_PLIST_NAME:String{
    return IS_DEBUG ? "d_GoogleService-Info" : "GoogleService-Info"
  }

  private override init() {
    super.init()
    guard let path = Bundle.main.path(forResource: MyAppFirebase.FIREBASE_PLIST_NAME, ofType: "plist") else {
      print("invalid firebase plist file : \(MyAppFirebase.FIREBASE_PLIST_NAME)")
      return
    }
    guard let firbaseOptions = FirebaseOptions(contentsOfFile: path) else {
      print("not make firbaseOptions : \(MyAppFirebase.FIREBASE_PLIST_NAME)")
      return
    }
    FirebaseApp.configure(options: firbaseOptions)
  }

}

使う時はこんな感じ

AppDelegate.swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
   //初期化だけするよ
    _ = MyFirebase.shared
  }
1
1
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
1
1