我在SwiftUI应用中收到一个无声的远程通知。它不是由UNUserNotificationCenter获取的,而是由旧的AppDelegate didReceiveNotification函数获取的。通知ContentView()发生更改的解决方案是什么? AppDelegate中的ObservableObject?
答案 0 :(得分:2)
您可以在AppDelegate中声明一个符合.as-console-wrapper { max-height: 100% !important; top: 0; }
的商店,并将其设置为ContentView的环境对象。
ObservableObject
// AppDelegate.swift
// in the class
let store = Store()
// in the applicationDidFinishLaunching(_:) method
window.contentView = NSHostingView(rootView: contentView.environmentObject(store))
环境对象是一个引用,因此您可以将值传递给它。与使用// ContentView.swift
// in the struct
@EnvironmentObject var store: Store
相同。完成更新后,只需调用ObservableObject
或将该属性标记为@Published。并在收到通知后更新ContentView。