通过SwiftUI接收APNS后显示视图 [英] Display a view after receiving APNS with SwiftUI

查看:62
本文介绍了通过SwiftUI接收APNS后显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用收到APNS推送通知后,我想显示一个视图.我正在使用SwiftUI.我遵循了本教程(https://blckbirds.com/post/how-to-navnigate-between-views-in-swiftui-by-using-an-environmentobject/)来创建motherView和ViewRouter.

I want to show a view, after my App received an APNS-Push Notification. I'm using SwiftUI. I followed this tutorial (https://blckbirds.com/post/how-to-navnigate-between-views-in-swiftui-by-using-an-environmentobject/) to create a motherView and a ViewRouter.

MotherView看起来像这样:

MotherView looks like that:

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

ViewRouter is a ObservableObjectClass

class ViewRouter: ObservableObject {

    let objectWillChange = PassthroughSubject<ViewRouter,Never>()

    var currentPage: Constants.Views = Constants.Views.login {
        didSet {
            objectWillChange.send(self)
        }
    }
}

在收到推送通知后,AppDelegate调用此函数:

The AppDelegate calls this function, after receiving a Push-Notification:

func presentView(with pushNotification: [String: AnyObject]) {
    //Here I want to set the viewRouter.currentPage = Constants.View.push
}

您对解决此问题有何建议?

What would be your suggestions to solve this problem?

推荐答案

这里是可行的方法.

1)使用 ViewRouter 中的本机合并发布为

1) Use native Combine publishing in ViewRouter as

class ViewRouter: ObservableObject {
    @Published var currentPage: Constants.Views = Constants.Views.login
}

2)在 AppDelegate

class AppDelegate {
   var viewRouter = ViewRouter()

...

func presentView(with pushNotification: [String: AnyObject]) {
    // set here new value
    self.viewRouter.currentPage = Constants.View.push

}

3)在 SceneDelegate 中,将 AppDelegate 中的 viewRouter 用作 ContentView (或MotherView ,如果您将其用作根视图)

3) In SceneDelegate use viewRouter from AppDelegate as an environment object for ContentView (or MotherView, if you use it as 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)

因此,您使用一个在 AppDelegate 中修改并在 MotherView 中处理的 ViewRoute 对象.

Thus, you use one ViewRoute object modified in AppDelegate and handled in MotherView.

这篇关于通过SwiftUI接收APNS后显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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