0
0
iOS Swiftmobile~20 mins

Analytics and Crashlytics in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: AnalyticsCrashlyticsDemo
This screen demonstrates basic integration of Firebase Analytics and Crashlytics in an iOS app. It tracks button taps as analytics events and simulates a crash to test Crashlytics reporting.
Target UI
-------------------------
| Analytics & Crashlytics |
|-----------------------|
| [Log Event Button]     |
|                       |
| [Cause Crash Button]   |
-------------------------
Add a button labeled 'Log Event Button' that logs a custom analytics event named 'button_tapped' with a parameter 'button_name' set to 'log_event'.
Add a button labeled 'Cause Crash Button' that triggers a crash to test Crashlytics.
Initialize Firebase in the app.
Ensure buttons are accessible with proper labels.
Use SwiftUI for UI implementation.
Starter Code
iOS Swift
import SwiftUI
import Firebase

@main
struct AnalyticsCrashlyticsDemoApp: App {
  init() {
    FirebaseApp.configure()
  }

  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

struct ContentView: View {
  var body: some View {
    VStack(spacing: 20) {
      // TODO: Add Log Event Button here
      // TODO: Add Cause Crash Button here
    }
    .padding()
  }
}
Task 1
Task 2
Task 3
Solution
iOS Swift
import SwiftUI
import Firebase

@main
struct AnalyticsCrashlyticsDemoApp: App {
  init() {
    FirebaseApp.configure()
  }

  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

struct ContentView: View {
  var body: some View {
    VStack(spacing: 20) {
      Button("Log Event Button") {
        Analytics.logEvent("button_tapped", parameters: ["button_name": "log_event"])
      }
      .accessibilityLabel("Log Event Button")
      .buttonStyle(.borderedProminent)

      Button("Cause Crash Button") {
        fatalError("Test Crash triggered")
      }
      .accessibilityLabel("Cause Crash Button")
      .buttonStyle(.bordered)
    }
    .padding()
  }
}

We start by configuring Firebase in the app's initializer to enable Analytics and Crashlytics.

The UI uses SwiftUI with two buttons stacked vertically.

The first button logs a custom event named button_tapped with a parameter button_name set to log_event. This helps track user interaction in Analytics.

The second button triggers a crash using fatalError to test Crashlytics crash reporting.

Both buttons have accessibility labels for screen readers and use SwiftUI's button styles for clear visual distinction.

Final Result
Completed Screen
-------------------------
| Analytics & Crashlytics |
|-----------------------|
| [Log Event Button]     |
|                       |
| [Cause Crash Button]   |
-------------------------
Tapping 'Log Event Button' sends a custom event to Firebase Analytics.
Tapping 'Cause Crash Button' immediately crashes the app to test Crashlytics reporting.
Stretch Goal
Add a Text view that shows the count of how many times the 'Log Event Button' was tapped during the current app session.
💡 Hint
Use a @State variable to track the tap count and update it inside the button's action closure.