38

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
  • Does this answer your question? SwiftUI life-cycle iOS14 Where to put AppDelegate code? Commented Jul 16, 2020 at 22:23
  • Actually the answer given by Peter Friese below is correct. It highlights the AppDelegate method and the way to do it in the new SwiftUI 2.0 framework as well. Commented Jul 17, 2020 at 6:38

2 Answers 2

90

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 } } 
Sign up to request clarification or add additional context in comments.

6 Comments

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?
I am using second approach, but can't setup Firebase Cloud Messaging. Push notifications is not showing, can anyone help me?
You'll need to use the third approach (@ UIApplicationDelegateAdaptor).
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
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?
|
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 Comments

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?
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.
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.