Dave Jha

Tutorial

Integrating Firebase Authentication into iOS 14/Xcode 12 with SwiftUI

Prist Team
bees
Firebase Authentication image provided by Google

[ The original post was written on Medium, and it can be found here ]

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
@main
struct YourProject: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct YourProjectApp_Previews: PreviewProvider {
static var previews: some View {
Text("Hello, World!")
}
}
  1. 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.

@main
struct 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!

© 2021 — Designed & developed by Dave Jha