Subchapter 5.5
references/ios_setup.mdMarkdown4 KBView on GitHub
When writing or updating SwiftUI code, you MUST prioritize the modern Swift
Observation framework (@Observable macro and @State) as your default
approach.
However, it is acceptable to use Combine (ObservableObject, @Published,
@StateObject, @EnvironmentObject) under the following conditions:
If neither of those conditions are true, default to the Swift 5.9+ Observation framework.
When using SwiftUI, you MUST ensure FirebaseApp.configure() is called
BEFORE any Firebase-dependent state objects are initialized.
@State (for @Observable) or @StateObject
(for Combine) property in the root App struct if its initializer touches
Firebase. Property initializers run before the App.init() body, meaning
the object’s init() will fire before Firebase is configured.App.init() and pass your state objects into
the sub-views (like ContentView), or use onAppear for delayed setup.Failing to follow this will result in a fatal crash:
Default FirebaseApp is not configured.
Do not use the Firebase Console. Use the CLI to automate setup:
npx -y firebase-tools@latest projects:create.pbxproj or Info.plist) to determine the
iOS bundle ID.npx -y firebase-tools@latest apps:create IOS <bundle-id>npx -y firebase-tools@latest apps:sdkconfig IOS <App-ID>GoogleService-Info.plist in your Xcode project folder.
Ensure you remove any non-XML CLI output headers, and ensure the file is
linked to the main application target.Do not use raw text parsing, sed, or Ruby scripts (like xcodeproj gem) to
modify .pbxproj files directly.
Instead, use the xcode-project-setup skill. Load that skill using your
tools to securely execute its native Swift package setup script. That skill
handles installing the required SPM packages and safely linking the
GoogleService-Info.plist file.
💡 TIP: ALWAYS USE THE LATEST SDK VERSION To ensure access to the latest features and security fixes, always check for the most recent version of the Firebase iOS SDK at https://github.com/firebase/firebase-ios-sdk/releases (opens in a new tab) and use that version when adding the SPM dependency.
Configure the shared FirebaseApp instance. You can do this either in a modern
SwiftUI App structure or a traditional AppDelegate.
import SwiftUI
import FirebaseCore
@main
struct YourApp: App {
// ⛔️ FATAL CRASH: @State private var auth = AuthManager()
// property initializers run before init(), causing FirebaseApp not configured error
@State private var authManager: AuthManager
init() {
// ✅ SAFE: This runs FIRST
FirebaseApp.configure()
// ✅ SAFE: Initialize state ONLY AFTER Firebase is configured
_authManager = State(initialValue: AuthManager())
}
var body: some Scene {
WindowGroup {
ContentView()
.environment(authManager)
}
}
}import UIKit
import FirebaseCore
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// ✅ SAFE: Always the first line in didFinishLaunching
FirebaseApp.configure()
return true
}
}