Notes on Getting Stuck When Introducing Firebase to an iOS Project

Notes on getting stuck when introducing Firebase to an iOS project.


Package Manager

Referring to Add Firebase to your Apple project - Google, currently Xcode 12.0 or later and CocoaPods 1.9.0 or later are recommended, so use Cocoapods instead of Swift Packaging Manager.

Handling Property List

When you want to separate the plist obtained when adding an App in the Firebase console for each environment. Rename each for each Firebase project and place them in the iOS project.

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)

Introducing Crashlytics

It is necessary to place the plist in the App built in the Build Phase.

Get started with Firebase Crashlytics - Google

Since it needs to be run at the end of the build, define it in the 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)']

And before that, copy the plist for each environment to the specified path in the App. Add a script to the Codegen of XCode for this.

1# Type a script or drag a script file from your workspace to insert its path.
2
3# Need to copy GoogleService-Info.plist to the build directory for Crashlytics
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

In the case of Firebase App Distribution, turn off Rebuild from Bitcode when archiving. This seems to eliminate the need for uploading because dSYM is bundled. When uploading to App Store Connect,

Firebase Analytics

Define various event types as follows:

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:

However, since it takes time for Firebase Analytics Events to be reflected, use the DebugView feature. In Xcode, set -FIRDebugEnabled as an argument in the Arguments tab of Product -> Scheme -> Edit scheme. Then, you can check events while debugging with a lag of several tens of seconds.

References

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