diff --git a/ios/ClerkAuthNativeView.swift b/ios/ClerkAuthNativeView.swift index e76a8be1b1c8faa64ec6dfa83764764094133aff..17b36ad1319765e2b6db0551d32e07d7140e482f 100644 --- a/ios/ClerkAuthNativeView.swift +++ b/ios/ClerkAuthNativeView.swift @@ -108,7 +108,12 @@ public class ClerkAuthNativeView: ClerkNativeViewHost { override func makeHostedController() -> UIViewController? { let hostBackAction: (() -> Void)? = currentHostBackButton - ? { [weak self] in self?.onHostBack([:]) } + ? { [weak self] in + guard let self else { return } + if !self.popEnclosingNavigationRoute() { + self.onHostBack([:]) + } + } : nil return ClerkNativeBridge.shared.makeAuthViewController( diff --git a/ios/ClerkNativeViewHost.swift b/ios/ClerkNativeViewHost.swift index 0d91f0e749f121595c17bc803663df6ac90e4163..8f8a89df97a54168b8b45d0e9c197ca3953b7028 100644 --- a/ios/ClerkNativeViewHost.swift +++ b/ios/ClerkNativeViewHost.swift @@ -5,6 +5,7 @@ public class ClerkNativeViewHost: ExpoView { private lazy var hostingCoordinator = ClerkNativeHostingCoordinator(containerView: self) private var hasInitialized: Bool = false private var configuredObserver: NSObjectProtocol? + private var isPoppingHostRoute = false public required init(appContext: AppContext? = nil) { super.init(appContext: appContext) @@ -58,6 +59,30 @@ public class ClerkNativeViewHost: ExpoView { func hostedViewDidDetachFromWindow() {} + /// Pops the React Navigation route that contains this view without waiting for + /// the JavaScript event loop. React Native Screens reports the native dismissal + /// back to React Navigation so its state remains synchronized. + func popEnclosingNavigationRoute() -> Bool { + guard !isPoppingHostRoute else { return true } + + var responder: UIResponder? = self + + while let nextResponder = responder?.next { + if let viewController = nextResponder as? UIViewController, + let navigationController = viewController.navigationController, + navigationController.viewControllers.count > 1 { + isPoppingHostRoute = true + if navigationController.popViewController(animated: true) != nil { + return true + } + isPoppingHostRoute = false + } + responder = nextResponder + } + + return false + } + private func addConfiguredObserver() { guard configuredObserver == nil else { return } diff --git a/ios/ClerkUserProfileNativeView.swift b/ios/ClerkUserProfileNativeView.swift index 12d6248b1dc4b4779b252c9954c2f89145907310..d838283b7adcf49fec2a63bff53251b9bee31bf8 100644 --- a/ios/ClerkUserProfileNativeView.swift +++ b/ios/ClerkUserProfileNativeView.swift @@ -44,7 +44,12 @@ public class ClerkUserProfileNativeView: ClerkNativeViewHost { override func makeHostedController() -> UIViewController? { let hostBackAction: (() -> Void)? = currentHostBackButton - ? { [weak self] in self?.onHostBack([:]) } + ? { [weak self] in + guard let self else { return } + if !self.popEnclosingNavigationRoute() { + self.onHostBack([:]) + } + } : nil return ClerkNativeBridge.shared.makeUserProfileViewController(