ios – How to open view on notification tapped using SwiftUI with proper hierarchy of views and go back to main application

0
25

[ad_1]

At this moment I managed to open the view, but not in the way I expected. I managed to open the view in several ways, the first of which was using the internal notification NotificationCenter.default.publisher(for: NSNotification.Name("OrderReviewView")) . After receiving it in the ContentView class, I set the variable bool showReviewOrder. The view was opening but I couldn’t roll back from the view to the Main class. Another way is presented below, the view also opens, but as in the first case, I can’t go back to the main application class. I tried to add NavigationStack by changing the line let host = UIHostingController(rootView: NavigationStack {orderWriteScreen.navigationBarBackButtonHidden(true))} and NavigationLink to the OrderReviewScreen and structure as follow

NavigationStack {
             ZStack {
                 VStack(alignment: .leading){
                     Text("Test")
                 }
                
                 NavigationLink(destination: ContentView())
                 {FAB() }
                     .padding(EdgeInsets(top: 0, leading: 0, bottom: 50, trailing: 20))
                     .frame(maxHeight: .infinity, alignment: .bottom)
                     .frame(maxWidth: .infinity, alignment: .trailing)
             }
            
         }

I was able to go back to the main application class, but there was a back button at the top of the screen and the hierarchy was still one window too many. I am attaching minimal working example of my code to show the problem.

@main
struct App: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
                ContentView()
        }
    }
}


struct ContentView: View {
    var body: some View {
        NavigationStack {
          if authManager.authorized == nil {
              SignInScreen()
          } else {
              Main()     
          }
        }
    }
}


struct Main: View {
    var body: some View {
        TabBar()
    }
}

extension TabBar {
    var baseTabBar: some View {
        TabView(selection: $selection) {
            OrderScreen()
                .tag(Tab.contracts)

        }
        .padding(.bottom, _displayTabBar ? tabBarHeight : 0)
    }
}

struct TabBar: View {
    let tabBarHeight: CGFloat = 55
    @State private var selection = Tab.farms
    @State var _displayTabBar = true
    
    init() {
        UITabBar.appearance().isHidden = true
    }
    var body: some View {
        ZStack(alignment: .bottom) {
            baseTabBar
                .environment(\.displayTabBar, displayTabBar)
            
            if (_displayTabBar) {
                styledTabBar
            }
        }
    }
}


struct OrderScreen: View {
    var body: some View {
      ZStack {
        VStack(alignment: .leading) {
            
        }
    }
}
struct OrderReviewScreen: View {
    var body: some View {
      VStack(alignment: .leading) {

    }
}


class AppDelegate: NSObject, UIApplicationDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

        FirebaseApp.configure()
        FirebaseConfiguration.shared.setLoggerLevel(.debug)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
          options: authOptions) { _, _ in }
        application.registerForRemoteNotifications()
        Messaging.messaging().delegate = self
        return true
    }
 }
extension AppDelegate: UNUserNotificationCenterDelegate {
    func application(
      _ application: UIApplication,
      didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
    ) {
      Messaging.messaging().apnsToken = deviceToken
    }

  func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
  ) {
      let userInfo = response.notification.request.content.userInfo
      let info = userInfo as NSDictionary
      guard let screen = info.value(forKey: "screen") as? String else { return }
      guard let id = info.value(forKey: "id") as? String else { return }
      notificationManager?.notifyId = id
      notificationManager?.notifyScreen = screen
      if screen == "order_details" {
          let orderWriteScreen = OrderReviewScreen()
          let host = UIHostingController(rootView: orderWriteScreen.navigationBarBackButtonHidden(true))
          UIApplication.shared.windows.first?.rootViewController = host
      }
    completionHandler()
  }
}
}

[ad_2]