Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to configure Firebase in my iOS app in the new SwiftUI App life cycle without AppDelegate and SceneDelegate?

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.

like image 565
thenakulchawla Avatar asked Jun 28 '20 18:06

thenakulchawla


People also ask

What is SwiftUI app lifecycle?

It's an iOS app's “main” function, the first one called during the lifetime of the app. You typically use it for: Initial configuration of 3rd-party components and frameworks, such as setting up Firebase, Bugsnag, Realm, OneSignal, Parse Server, Reachability services, and so on.

What is the use of firebase in iOS?

Firebase is a mobile backend-as-a-service that provides powerful features for building mobile apps. Firebase has three core services: Realtime database. User authentication.


1 Answers

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   } } 
like image 153
Peter Friese Avatar answered Oct 22 '22 18:10

Peter Friese