iOSプロジェクトへのFirebase導入で詰まったことメモ

Category:Tech BlogTags:
#iOS#Firebase
Published: 2020 - 11 - 30

パッケージマネージャ

Firebase を iOS プロジェクトに追加する - Googleを参照すると,現状ではXcode 12.0 以降,CocoaPods 1.9.0 以降が推奨されているので,Swift Packaging ManagerではなくCocoapodsを使う.

Property
Listの扱い

FirebaseのconsoleでのApp追加時に取得するplistを環境毎に分けたい場合.それぞれFirebaseのプロジェクト毎にリネームし,iOSプロジェクト内に配置.

1// AppDelegate.swift
2
3// MARK: - Firebase init
4let configFileName: String
5#if DEBUG
6configFileName = "GoogleService-dev-Info"
7#else
8configFileName = "GoogleService-prod-Info"
9#endif
10guard let filePath = Bundle.main.path(forResource: configFileName, ofType: "plist"),
11    let options = FirebaseOptions(contentsOfFile: filePath) else {
12    fatalError("Firebase plist file is not found.")
13}
14FirebaseApp.configure(options: options)

Crashlystics導入

Build PhaseでビルドしたApp内にplistを配置する必要がある.

Get started with Firebase Crashlytics - Google

ビルド時の最後に走らせる必要があるので,Podfileに定義する.

1# Podfile
2
3target 'PROJECT_NAME' do
4  # Comment the next line if you don't want to use dynamic frameworks
5  use_frameworks!
6
7  pod 'Firebase/Crashlytics'
8  script_phase :name=> 'FirebaseCrashlytics',
9                 :script=> '"${PODS_ROOT}/FirebaseCrashlytics/run"',
10                 :input_files=> ['$(SRCROOT)/$(BUILT_PRODUCTS_DIR)/$(INFOPLIST_PATH)']

そしてその前に各環境毎のplistをApp内の指定したパスにコピーしておく.lpれはXCodeのCodegen上にスクリプトを足しておく.

1# Type a script or drag a script file from your workspace to insert its path.
2
3# Crashlytics用にGoogleService-Info.plistをビルドディレクトリにコピーする必要があるた
4PATH_TO_GOOGLE_PLISTS="${PROJECT_DIR}/bengoshi-ios/Firebase"
5
6case "${CONFIGURATION}" in
7"Debug" )
8cp -r "$PATH_TO_GOOGLE_PLISTS/GoogleService-dev-Info.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist" ;;
9
10"Release" )
11cp -r "$PATH_TO_GOOGLE_PLISTS/GoogleService-prod-Info.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist" ;;
12
13*)
14;;
15esac

Firebase App Distributionの場合はアーカイブ時に Rebuild from Bitcodeをオフにする.これによりdSYMが同梱されるためアップロードの必要がなくなるらしい. App Store Connectにアップロードする際は,

以下のように各種イベントの種類等定義しておく

1import Foundation
2import Firebase
3import FirebaseAnalytics
4
5class EventLogs {
6    // MARK: - LogIn
7    static func loginInputTelNumber(success: Bool, error_type: String? = nil) {
8        if success {
9            Analytics.logEvent("ios_login_input_tel_number_succeed",
10                               parameters: nil)
11        } else {
12            Analytics.logEvent("ios_login_input_tel_number_failure",
13                               parameters: ["error_type": error_type as Any])
14        }
15    }
16    static func login(success: Bool, error_type: String? = nil) {
17        if success {
18            Analytics.logEvent("login", parameters: ["method": "iOS"])
19            Analytics.logEvent("ios_login_succeed", parameters: nil)
20        } else {
21            Analytics.logEvent("ios_login_failure",
22                               parameters: ["error_type": error_type as Any])
23        }
24    }
25    static func logout() {
26        Analytics.logEvent("ios_logout", parameters: nil)
27    }
28    // MARK: - UI
29    static func openModal() {
30        Analytics.logEvent("ios_ui_modal_open", parameters: nil)
31    }
32    static func closeModal() {
33        Analytics.logEvent("ios_ui_modal_close", parameters: nil)
34    }
35:
36:

ただ,Firabse AnaltyicsのEventは反映されるのに時間がかかるので,DebugView 機能を用いる.Xcodeで、Product -> Scheme -> Edit schemeのArgumentsタブで -FIRDebugEnabledを引数に設定する.すると,数十秒のラグでデバッグしながらイベントを確認できる.

参考

https://www.apps-gcp.com/introduction-of-firebase-analytics/#i-5

他の記事を読む