ios - 使用 SwiftUI 接收 APNS 后显示 View

标签 ios swift swiftui apple-push-notifications

在我的应用收到 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)

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

关于ios - 使用 SwiftUI 接收 APNS 后显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60490860/

相关文章:

来自 URL 的 iOS CoreSpotlight 缩略图

swift - 在主视图 Controller 中添加 subview 无法执行按钮单击事件

swiftui - 如何使用 NavigationLink 在列表行中拥有其他可选区域?

iphone - iOS应用最大的iOS版本支持

ios - 多线程 iOS 中的通知

用于返回接收者类型(符合协议(protocol))的协议(protocol)的 Swift 二元运算符

swift - 尝试用 self 初始化结构会出错。正确的做法是什么?

SwiftUI - @State 属性未更新

ios - View 关闭后 ObservedObject View 模型仍在内存中

ios - 为 React Native 应用程序开发模拟慢速互联网连接