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.
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.
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.
There are three approaches for initialising third part frameworks in the new SwiftUI life cycle:
You can still use the old life cycle model:
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.
If you want to use the new life cycle model, use either one of the following approaches.
App
's initialiserYou 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() } } }
@ 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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With