1

I am experiencing an issue when deep linking into a certain SwiftUI view from a link. the openTakeVC is what is called when deep linked. Currently it was to be embedded in a UINavigationController in order to work, if I try just presenting the UIHostingController I get a crash with this error:

Thread 1: "Application tried to present modally a view controller <_TtGC7SwiftUI19UIHostingControllerV8uSTADIUM8TakeView_: 0x14680a000> that has a parent view controller <UINavigationController: 0x1461af000>."

The dismiss functionality works perfectly fine if not embedded in a UINavigationController but I am only able to deep link that view using a UINavigationController.

Is there a fix for this error or a way to dismiss a UIHostingController embedded in a UINavigationController?

func openTakeVC(take: TakeOBJ) {
            DispatchQueue.main.async {
                
                guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
                if let _ = appDelegate.window?.rootViewController as? BannedViewController { return }
                
                //let vc = TakeSingleViewController(nibName: "TakeSingleView", bundle: nil, take: take)
                let vc = UIHostingController(rootView: TakeView(take: take))
                let nav = UINavigationController(rootViewController: vc)
                nav.modalPresentationStyle = .fullScreen
                nav.setNavigationBarHidden(true, animated: false)
                
                appDelegate.window?.rootViewController?.present(vc, animated: true, completion: nil)
                UserDefaults.removeURLToContinue()
            }
        }

in TakeView

@Environment(\.presentationMode) var presentationMode


  Button {
     UIImpactFeedbackGenerator(style: .light).impactOccurred()
     presentationMode.wrappedValue.dismiss()
                        
     } label: {
        Image(systemName: "xmark")
     }
  }
Noah Iarrobino
  • 1,435
  • 1
  • 10
  • 31

1 Answers1

0

What about creating the hostingcontroller as a separate vc and dismissing it from there?

TakeSingleVC:

final class TakeSingleVC: UIViewController {
    
    var viewModel: TakeViewModel
    var subscriptions = Set<AnyCancellable>()
    
    init(viewModel: TakeViewModel) {
        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let childView = UIHostingController(rootView: TakeView(viewModel: viewModel))
        addChild(childView)
        childView.view.frame = view.bounds
        view.addSubview(childView.view)
        childView.didMove(toParent: self)
        
        viewModel.dismissSheet
            .sink { isDismissed in
                if isDismissed {
                    childView.dismiss(animated: true)
                }
            }.store(in: &subscriptions)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

Dismissing in TakeView

viewModel.dismissSheet.send(true)

TakeViewModel:

final class TakeViewModel: ObservableObject {
    
    // UIKit
    var dismissSheet = CurrentValueSubject<Bool, Never>(false)
}

and then change your presentation to

let vc = TakeSingleVC(viewModel: viewModel)
let nav = UINavigationController(rootViewController: vc)
Thel
  • 396
  • 2
  • 8