在没有 AppDelegate 和 SceneDelegate 的新 SwiftUI 应用程序生命周期中,在哪里可以在我的 iOS 应用程序中配置 Firebase? [英] Where to configure Firebase in my iOS app in the new SwiftUI App life cycle without AppDelegate and SceneDelegate?

查看:21
本文介绍了在没有 AppDelegate 和 SceneDelegate 的新 SwiftUI 应用程序生命周期中,在哪里可以在我的 iOS 应用程序中配置 Firebase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个可以完全使用 SwiftUI 构建的应用程序.我使用 Firebase 使用 Cloud Functions 进行身份验证和通知.

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.

现在使用新的 SwiftUI App->Scene->View 构造,我无法将设置添加到我的应用中.

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

例如 ->最初的 FirebaseApp.configure() 最初会放在 AppDelegatedidFinishLaunchingWithOptions 中,现在我不知道在哪里添加这个配置.

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:如果需要前一个应用程序的更多详细信息/代码,请发表评论.我没有添加任何代码,因为我觉得没有必要.

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.

推荐答案

在新的 SwiftUI 生命周期中初始化第三方框架有三种方法:

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

你仍然可以使用旧的生命周期模型:

You can still use the old life cycle model:

创建新的 SwiftUI 项目时,您可以选择旧的生命周期模型.这将像以前一样创建一个 AppDelegate 和一个 SceneDelegate.我承认并不像一直使用 SwiftUI 那样花哨 - 但绝对是最简单、最直接的方法.

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 类的默认初始化程序,如下所示:

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()
    }
  }
}

选项3:使用@ UIApplicationDelegateAdaptor

在您的 App 类中,定义一个属性来保存对您的 AppDelegate 的引用,并让 SwiftUI 使用 AppDelegate 注入 AppDelegate>@ UIApplicationDelegateAdaptor 属性包装器,像这样:

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
  }
}

这篇关于在没有 AppDelegate 和 SceneDelegate 的新 SwiftUI 应用程序生命周期中,在哪里可以在我的 iOS 应用程序中配置 Firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆