Notes on Getting Stuck When Introducing Firebase to an iOS Project

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

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.

// AppDelegate.swift

// MARK: - Firebase init
let configFileName: String
#if DEBUG
configFileName = "GoogleService-dev-Info"
#else
configFileName = "GoogleService-prod-Info"
#endif
guard let filePath = Bundle.main.path(forResource: configFileName, ofType: "plist"),
    let options = FirebaseOptions(contentsOfFile: filePath) else {
    fatalError("Firebase plist file is not found.")
}
FirebaseApp.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.

# Podfile

target 'PROJECT_NAME' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  pod 'Firebase/Crashlytics'
  script_phase :name=> 'FirebaseCrashlytics',
                 :script=> '"${PODS_ROOT}/FirebaseCrashlytics/run"',
                 :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.

# Type a script or drag a script file from your workspace to insert its path.

# Need to copy GoogleService-Info.plist to the build directory for Crashlytics
PATH_TO_GOOGLE_PLISTS="${PROJECT_DIR}/bengoshi-ios/Firebase"

case "${CONFIGURATION}" in
"Debug" )
cp -r "$PATH_TO_GOOGLE_PLISTS/GoogleService-dev-Info.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist" ;;

"Release" )
cp -r "$PATH_TO_GOOGLE_PLISTS/GoogleService-prod-Info.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist" ;;

*)
;;
esac

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,

Define various event types as follows:

import Foundation
import Firebase
import FirebaseAnalytics

class EventLogs {
    // MARK: - LogIn
    static func loginInputTelNumber(success: Bool, error_type: String? = nil) {
        if success {
            Analytics.logEvent("ios_login_input_tel_number_succeed",
                               parameters: nil)
        } else {
            Analytics.logEvent("ios_login_input_tel_number_failure",
                               parameters: ["error_type": error_type as Any])
        }
    }
    static func login(success: Bool, error_type: String? = nil) {
        if success {
            Analytics.logEvent("login", parameters: ["method": "iOS"])
            Analytics.logEvent("ios_login_succeed", parameters: nil)
        } else {
            Analytics.logEvent("ios_login_failure",
                               parameters: ["error_type": error_type as Any])
        }
    }
    static func logout() {
        Analytics.logEvent("ios_logout", parameters: nil)
    }
    // MARK: - UI
    static func openModal() {
        Analytics.logEvent("ios_ui_modal_open", parameters: nil)
    }
    static func closeModal() {
        Analytics.logEvent("ios_ui_modal_close", parameters: nil)
    }
:
:

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

Read more articles