Integrating Firebase Authentication into iOS 14/Xcode 12 with SwiftUI
Introduction
Google’s Firebase documentation has not been updated to reflect the new changes coming in Xcode 12 with the removal of App Delegate. This article is meant to be a quick explanation of how to integrate some of App Delegate back into your project, so you can utilize the full API’s Firebase gives you.
Getting Started
Navigate to your project’s YourProjectApp.swift. This is the entrance point to your application. It should have something like this:
import SwiftUI
@mainstruct YourProject: App { var body: some Scene { WindowGroup { ContentView() } }}struct YourProjectApp_Previews: PreviewProvider { static var previews: some View { Text("Hello, World!") }}
- Import Firebase and UIKit into YourProjectApp.swift
import Firebase
import UIKit // We will need UIKit to implement AppDelegate
2. Create an AppDelegate class and didFinishLaunchingWithOptions function with FirebaseApp.configure()
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// Configure FirebaseApp
FirebaseApp.configure()
return true
}
}
3. Finally, create a reference to the App Delegate within the YourProject struct, not the previews.
@mainstruct BizeeiOS: App { // Create a reference to the App Delegate @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } }}
And you are done! It’s as simple as that. Firebase will initialize every time your app runs.
Happy coding!