在我的应用收到 APNS 推送通知后,我想显示一个 View 。我正在使用 SwiftUI。我按照本教程 (https://blckbirds.com/post/how-to-navnigate-between-views-in-swiftui-by-using-an-environmentobject/) 创建了一个 MotherView 和一个 ViewRouter。
MotherView 看起来像这样:
struct MotherView: View {
// MARK: - Properties
@EnvironmentObject var viewRouter: ViewRouter
@State private var isMenuVisible: Bool = false
var body: some View {
VStack {
if viewRouter.currentPage == Constants.Views.login {
Login_SwiftUIView()
} else if viewRouter.currentPage == Constants.Views.main {
MainView(withMenu: $isMenuVisible)
} else if viewRouter.currentPage == Constants.Views.menu {
Menu_SwiftUI(withMenu: $isMenuVisible)
} else if viewRouter.currentPage == Constants.Views.push {
PushView()
}
}
}
}
ViewRouter 是一个 ObservableObjectClass
class ViewRouter: ObservableObject {
let objectWillChange = PassthroughSubject<ViewRouter,Never>()
var currentPage: Constants.Views = Constants.Views.login {
didSet {
objectWillChange.send(self)
}
}
}
AppDelegate 在收到 Push-Notification 后调用此函数:
func presentView(with pushNotification: [String: AnyObject]) {
//Here I want to set the viewRouter.currentPage = Constants.View.push
}
你对解决这个问题有什么建议?
最佳答案
这是可能的方法。
1) 在ViewRouter
中使用原生组合发布作为
class ViewRouter: ObservableObject {
@Published var currentPage: Constants.Views = Constants.Views.login
}
2) 创建
ViewRouter
的成员实例在 AppDelegate
class AppDelegate {
var viewRouter = ViewRouter()
...
func presentView(with pushNotification: [String: AnyObject]) {
// set here new value
self.viewRouter.currentPage = Constants.View.push
}
3) 在
SceneDelegate
使用 viewRouter
来自 AppDelegate
作为 ContentView
的环境对象(或 MotherView
,如果您将其用作 Root View )class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let contentView = ContentView().environmentObject(appDelegate.viewRoute)
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
因此,您使用一个
ViewRoute
在 AppDelegate
中修改的对象并在 MotherView
中处理.
关于ios - 使用 SwiftUI 接收 APNS 后显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60490860/