简体   繁体   English

如何从SwiftUI中的ObservableObject class访问AppDelegate中的数据

[英]How to access data in AppDelegate from ObservableObject class in SwiftUI

With the new SwiftUI Lifecycle there normally is no AppDelegate anymore.使用新的 SwiftUI 生命周期,通常不再有AppDelegate However, in order to implement Firebase Messaging, it is recommended on the Firebase docs to implement an AppDelegate and attach it using:但是,为了实现 Firebase 消息传递,建议在 Firebase 文档上实现AppDelegate并使用以下方法附加它:

@main
struct ExampleApp: SwiftUI.App {
    
    // register app delegate for Firebase setup
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate //<--- HERE
    
    @StateObject var appState = AppState()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(appState)
        }
    }
}

Inside this AppDelegate functions one obtains an FCM Token, with which the device is identified to subsequently send it notifications remotely.在这个AppDelegate函数中,人们获得了一个 FCM 令牌,通过该令牌可以识别设备,以便随后向其远程发送通知。 This token has to get sent to the server.此令牌必须发送到服务器。 This could be done inside the respective function inside the AppDelegate .这可以在AppDelegate内的相应 function 内完成。 However, there is a class AppState (ObservableObject protocol) that handles the user data and writing to the server, so it would make a lot more sense to write pass it to this class (or retrieve in the class from the AppDelegate ?) and then push it from there.但是,有一个 class AppState (ObservableObject 协议)处理用户数据并写入服务器,因此将它写入传递给这个 class(或从AppDelegate检索 class ?)然后然后从那里推动它。

How can this be accomplished?如何做到这一点?

edit: I guess this could be achieved by using a static property in the AppDelegate as described in this answer .编辑:我想这可以通过在AppDelegate中使用static属性来实现,如本答案中所述。 Is using statics to access variables globally not bad practice?使用静态变量全局访问变量不是坏习惯吗? Is there a other (better) way?还有其他(更好)的方法吗?

You can do it with the "old way" of accessing the delegate您可以使用访问delegate的“旧方法”来完成

@MainActor //Required by the app delegate
class AppState: ObservableObject{
    lazy var appDelegate: AppDelegate? = {
        UIApplication.shared.delegate as? AppDelegate
    }()
}

Then you can just use然后你就可以使用

appDelegate?.yourToken

yourToken referencing a property in the delegate yourToken引用delegate中的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 SwiftUI 上的可观察对象中使用可观察对象 class? - How can I use an observableobject class in an observableobject on SwiftUI? 如何使用 swiftui 从 appdelegate 访问 wkwebview? - how can i access wkwebview from appdelegate with swiftui? 如何从AppDelegate.m访问某些实例类变量 - How to access some instance class variables from AppDelegate.m 从AppDelegate访问其他类实例 - Access other Class instance from AppDelegate iOS SwiftUI:从父级到子级的 ObservableObject - iOS SwiftUI: ObservableObject from parent to child 从ViewController中的AppDelegate访问数据。 - Access data from AppDelegate in ViewController. Swift将数据从appDelegate传递到另一个类 - Swift Passing data from appDelegate to another class Singleton类DataLoader-从AppDelegate中的Core Data设置值,但是无法访问其他类中的DataLoader变量 - Singleton class DataLoader - set values from Core Data in AppDelegate, however cannot access DataLoader variables in other classes 如何从视图控制器类访问AppDelegate类中声明的对象? - How can I access an object declared in the AppDelegate class from my view controller class? 如何从 AppDelegate.m 访问 tabBarController? - How to access tabBarController from AppDelegate.m?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM