36

I have an app already that I was able to build completely with SwiftUI. I was using Firebase for authentication and notifications using Cloud Functions.

Now with the new SwiftUI App->Scene->View construct, I am unable to add the setup to my app.

For example -> The initial FirebaseApp.configure() would initially go in didFinishLaunchingWithOptions in AppDelegate, now I am at a loss of where to add this configuration.

Same goes with setting up remote notifications.

PS: Please comment if more details/code of the previous app is required. I did not add any code, cause I felt it was unnecessary.

2

2 Answers 2

83

There are three approaches for initialising third part frameworks in the new SwiftUI life cycle:

Using the old life cycle model

You can still use the old life cycle model:

Option 1: Use the UIKit App Delegate life cycle

When creating a new SwiftUI project, you can choose the old life cycle model. This will create an AppDelegate and a SceneDelegate as before. Not as fancy as using SwiftUI all the way, I admit - but definitely the easiest and most straightforward way.

Setting up a SwiftUI 2.0 project with the traditional AppDelegate Life Cycle

Using the new life cycle model

If you want to use the new life cycle model, use either one of the following approaches.

Option 2: Use the App's initialiser

You can override the default initialiser of your App class, like this:

import SwiftUI
import Firebase

@main
struct SO62626652_InitialiserApp: App {
  
  init() {
    FirebaseApp.configure()
  }
  
  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

Option 3: Use @ UIApplicationDelegateAdaptor

In you App class, define a property that holds a reference to your AppDelegate, and let SwiftUI inject the AppDelegate using the @ UIApplicationDelegateAdaptor property wrapper, like this:

import SwiftUI
import Firebase

@main
struct SO62626652_AppDelegateAdaptorApp: App {
  @UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

class AppDelegate: NSObject, UIApplicationDelegate {
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    FirebaseApp.configure()
    return true
  }
}
6
  • 2
    Thanks, @peter-friese, for all your help with this and similar issues here and everywhere else. Any difference between the second and third options? and if not, why would anyone use the last one? Also, why is such information not included in the official documentation yet?
    – amrabed
    Oct 4, 2020 at 20:40
  • I am using second approach, but can't setup Firebase Cloud Messaging. Push notifications is not showing, can anyone help me? Oct 5, 2020 at 8:04
  • 2
    You'll need to use the third approach (@ UIApplicationDelegateAdaptor). Oct 5, 2020 at 8:06
  • 2
    I've put together a more thorough write-up that shows how to initialise Firebase in SwiftUI 2 apps: peterfriese.dev/swiftui-new-app-lifecycle-firebase Oct 19, 2020 at 21:41
  • In your YT video, "Sign in with Apple using Firebase Authentication", at around 4:54, you have a line "authUI.delegate = self". Where does that line go if using either method 2 or method 3? Apr 10, 2021 at 17:56
0

Found the answer on the link below:

hackingWithSwift

The code from the page is below:

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print("Your code here")
        FirebaseApp.configure()
        return true
    }
}

And inside the App

we need to add the below line:

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate.

The explanation is on the link.

3
  • I've found this code snippet in many places, but the code I'm trying to refactor (Peter's YT video, "Sign in with Apple using Firebase Authentication") for the SwiftUI lifecycle has a line like "authUI.delegate = self" and I can't find where that is supposed to go. Any ideas? Apr 10, 2021 at 17:57
  • Can you try and post this in a more elaborate way. This requires me to go watch the video (link not provided) and try to find where you are finding the problem. Apr 12, 2021 at 16:48
  • Sorry. I thought the link was above. Peter's YT video is here: youtube.com/watch?v=BxQsdhglZtE. In that video, at around 4:53, there is a line of Swift code: authUI.delegate = self. Not sure where to put that. Or if it's no longer necessary. Does the @UIapplicationDelegateAdaptor code that you mentioned replace that? Apr 15, 2021 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.