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.

Peter Friese
  • 6,709
  • 31
  • 43
thenakulchawla
  • 5,024
  • 7
  • 30
  • 42
  • Does this answer your question? [SwiftUI life-cycle iOS14 Where to put AppDelegate code?](https://stackoverflow.com/questions/62538110/swiftui-life-cycle-ios14-where-to-put-appdelegate-code) – M1X Jul 16 '20 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. – thenakulchawla Jul 17 '20 at 06:38

2 Answers2

80

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
  }
}
Peter Friese
  • 6,709
  • 31
  • 43
  • 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? – A.A. Oct 04 '20 at 20:40
  • I am using second approach, but can't setup Firebase Cloud Messaging. Push notifications is not showing, can anyone help me? – Hattori Hanzō Oct 05 '20 at 08:04
  • 2
    You'll need to use the third approach (@ UIApplicationDelegateAdaptor). – Peter Friese Oct 05 '20 at 08:06
  • 2
    I've put together a more thorough write-up that shows how to initialise Firebase in SwiftUI 2 apps: https://peterfriese.dev/swiftui-new-app-lifecycle-firebase/ – Peter Friese Oct 19 '20 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? – Zonker.in.Geneva Apr 10 '21 at 17:56
  • It would be great if this tip can be added to the doc: https://firebase.google.com/docs/analytics/get-started?platform=ios#swift – yo1995 Apr 07 '22 at 18:45
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.

thenakulchawla
  • 5,024
  • 7
  • 30
  • 42
  • 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? – Zonker.in.Geneva Apr 10 '21 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. – thenakulchawla Apr 12 '21 at 16:48
  • Sorry. I thought the link was above. Peter's YT video is here: https://www.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? – Zonker.in.Geneva Apr 15 '21 at 12:53