The best way to control Claude Code, Codex and more from any computer or phone
| 1 | diff --git a/ios/ClerkAuthNativeView.swift b/ios/ClerkAuthNativeView.swift |
| 2 | index 5f8d2ba..b170304 100644 |
| 3 | --- a/ios/ClerkAuthNativeView.swift |
| 4 | +++ b/ios/ClerkAuthNativeView.swift |
| 5 | @@ -111,7 +111,12 @@ public class ClerkAuthNativeView: ClerkNativeViewHost { |
| 6 | |
| 7 | override func makeHostedController() -> UIViewController? { |
| 8 | let hostBackAction: (() -> Void)? = currentHostBackButton |
| 9 | - ? { [weak self] in self?.onHostBack([:]) } |
| 10 | + ? { [weak self] in |
| 11 | + guard let self else { return } |
| 12 | + if !self.popEnclosingNavigationRoute() { |
| 13 | + self.onHostBack([:]) |
| 14 | + } |
| 15 | + } |
| 16 | : nil |
| 17 | |
| 18 | return ClerkNativeBridge.shared.makeAuthViewController( |
| 19 | diff --git a/ios/ClerkUserProfileNativeView.swift b/ios/ClerkUserProfileNativeView.swift |
| 20 | index f2ca604..46f406d 100644 |
| 21 | --- a/ios/ClerkUserProfileNativeView.swift |
| 22 | +++ b/ios/ClerkUserProfileNativeView.swift |
| 23 | @@ -47,7 +47,12 @@ public class ClerkUserProfileNativeView: ClerkUserProfileCustomPageHost { |
| 24 | |
| 25 | override func makeHostedController() -> UIViewController? { |
| 26 | let hostBackAction: (() -> Void)? = currentHostBackButton |
| 27 | - ? { [weak self] in self?.onHostBack([:]) } |
| 28 | + ? { [weak self] in |
| 29 | + guard let self else { return } |
| 30 | + if !self.popEnclosingNavigationRoute() { |
| 31 | + self.onHostBack([:]) |
| 32 | + } |
| 33 | + } |
| 34 | : nil |
| 35 | |
| 36 | return ClerkNativeBridge.shared.makeUserProfileViewController( |
| 37 | diff --git a/ios/ClerkNativeViewHost.swift b/ios/ClerkNativeViewHost.swift |
| 38 | --- a/ios/ClerkNativeViewHost.swift |
| 39 | +++ b/ios/ClerkNativeViewHost.swift |
| 40 | @@ -5,6 +5,7 @@ |
| 41 | private lazy var hostingCoordinator = ClerkNativeHostingCoordinator(containerView: self) |
| 42 | private var hasInitialized: Bool = false |
| 43 | private var configuredObserver: NSObjectProtocol? |
| 44 | + private var isPoppingHostRoute = false |
| 45 | |
| 46 | public required init(appContext: AppContext? = nil) { |
| 47 | super.init(appContext: appContext) |
| 48 | @@ -23,6 +24,7 @@ |
| 49 | super.didMoveToWindow() |
| 50 | |
| 51 | guard window != nil else { |
| 52 | + isPoppingHostRoute = false |
| 53 | if hasInitialized { |
| 54 | hostedViewDidDetachFromWindow() |
| 55 | } |
| 56 | @@ -58,6 +60,39 @@ |
| 57 | |
| 58 | func hostedViewDidDetachFromWindow() {} |
| 59 | |
| 60 | + func popEnclosingNavigationRoute() -> Bool { |
| 61 | + guard !isPoppingHostRoute else { return true } |
| 62 | + |
| 63 | + // Follow controller containment, not the responder chain: past a presented controller the |
| 64 | + // responder chain continues to its presenter, so a host inside a modal would pop the screen |
| 65 | + // behind the modal instead of letting `onHostBack` dismiss it. |
| 66 | + var route = enclosingViewController(of: self) |
| 67 | + |
| 68 | + while let candidate = route { |
| 69 | + if let navigationController = candidate.parent as? UINavigationController, |
| 70 | + navigationController.viewControllers.count > 1 { |
| 71 | + guard navigationController.topViewController === candidate else { return false } |
| 72 | + |
| 73 | + isPoppingHostRoute = true |
| 74 | + guard navigationController.popViewController(animated: true) != nil else { |
| 75 | + isPoppingHostRoute = false |
| 76 | + return false |
| 77 | + } |
| 78 | + if let coordinator = navigationController.transitionCoordinator { |
| 79 | + coordinator.animate(alongsideTransition: nil) { [weak self] _ in |
| 80 | + self?.isPoppingHostRoute = false |
| 81 | + } |
| 82 | + } else { |
| 83 | + isPoppingHostRoute = false |
| 84 | + } |
| 85 | + return true |
| 86 | + } |
| 87 | + route = candidate.parent |
| 88 | + } |
| 89 | + |
| 90 | + return false |
| 91 | + } |
| 92 | + |
| 93 | private func addConfiguredObserver() { |
| 94 | guard configuredObserver == nil else { return } |
| 95 | |
| 96 | @@ -162,7 +197,7 @@ |
| 97 | controller.view.frame = containerView.bounds |
| 98 | controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
| 99 | |
| 100 | - if let parentVC = findViewController(from: containerView) { |
| 101 | + if let parentVC = enclosingViewController(of: containerView) { |
| 102 | parentVC.addChild(controller) |
| 103 | containerView.addSubview(controller.view) |
| 104 | controller.didMove(toParent: parentVC) |
| 105 | @@ -190,15 +225,15 @@ |
| 106 | guard let containerView else { return } |
| 107 | hostingController?.view.frame = containerView.bounds |
| 108 | } |
| 109 | - |
| 110 | - private func findViewController(from view: UIView) -> UIViewController? { |
| 111 | - var responder: UIResponder? = view |
| 112 | - while let nextResponder = responder?.next { |
| 113 | - if let vc = nextResponder as? UIViewController { |
| 114 | - return vc |
| 115 | - } |
| 116 | - responder = nextResponder |
| 117 | - } |
| 118 | - return nil |
| 119 | - } |
| 120 | -} |
| 121 | +} |
| 122 | + |
| 123 | +private func enclosingViewController(of view: UIView) -> UIViewController? { |
| 124 | + var responder: UIResponder? = view |
| 125 | + while let nextResponder = responder?.next { |
| 126 | + if let viewController = nextResponder as? UIViewController { |
| 127 | + return viewController |
| 128 | + } |
| 129 | + responder = nextResponder |
| 130 | + } |
| 131 | + return nil |
| 132 | +} |