SwiftUI NavigationStack Deep Link Handling Patterns
Mobile Development
Date:November 23, 2026
Topic:
⏱ 3 min read
You push a notification, the app launches, and instead of landing on the order detail screen, the user stares at your root tab view. Deep linking in SwiftUI used to be a maze of onOpenURL hacks and fragile NavigationLink chains. iOS 16 gave us NavigationStack and a path binding. iOS 18 sharpens the edges with state restoration APIs that finally make deep linking feel native instead of negotiated.
The Path Is State, Not Magic
Treat the navigation path as a single source of truth. A NavigationPath bound to a scene-level @StateObject survives tab switches, backgrounding, and process death. When a universal link arrives, you mutate the path. The stack renders the destination. No imperative pushes, no view-finding callbacks.
swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@MainActor finalclass AppRouter: ObservableObject {
@Published var path = NavigationPath()
func handle(_ url: URL) {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
components.scheme == "myapp",
let route = Route(url: url) else { return }
path.append(route)
}
}
struct Route: Hashable {
let type: RouteType
let id: String
enum RouteType: String, Hashable { case order, product, profile }
init?(url: URL) {
let parts = url.pathComponents.dropFirst()
guard let first = parts.first,
let type = RouteType(rawValue: first),
let id = parts.dropFirst().first else { return nil }
self.type = type; self.id = id
}
}
Decode at the Destination
Keep route objects lightweight. Pass only identifiers. Let the destination view fetch its own data. This decouples the router from your networking layer and makes previews trivial. Use .navigationDestination(for:) at the stack root to map each route type to its view factory.
swift
1
2
3
4
5
6
7
8
9
10
NavigationStack(path: $router.path) {
HomeView()
.navigationDestination(for: Route.self) { route in
switch route.type {
case .order: OrderDetailView(orderID: route.id)
case .product: ProductView(productID: route.id)
case .profile: ProfileView(userID: route.id)
}
}
}
💡
TipRegister multiple .navigationDestination modifiers for different Hashable types. The stack picks the first match. This lets feature modules register their own routes without a central enum.
State Restoration Without the Boilerplate
iOS 18 introduces stateRestorationActivity on NavigationStack. Encode the current path into an NSUserActivity and the system restores it on next launch — even after an OS update. Conform your route types to Codable and let JSONEncoder do the heavy lifting.
swift
1
2
3
4
5
6
7
8
9
10
11
12
13
NavigationStack(path: $router.path) {
// ... destinations
}
.stateRestorationActivity("com.myapp.navigation") { path in
let data = tryJSONEncoder().encode(path.map { $0 as! Route })
let activity = NSUserActivity(activityType: "com.myapp.navigation")
activity.userInfo = ["path": data]
return activity
} restore: { activity in
guard let data = activity.userInfo?["path"] as? Data,
let routes = try? JSONDecoder().decode([Route].self, from: data) else { return nil }
returnNavigationPath(routes)
}
Handling Cold Starts and Race Conditions
A universal link can arrive before your router initializes. Store the incoming URL in a singleton DeepLinkQueue during application(_:continue:restorationHandler:). When the router appears, drain the queue. This also handles links that arrive while the app is backgrounded but alive.
Unit test the router in isolation. Feed it URLs, assert the path contents. No ViewInspector needed. For UI tests, launch with -ui-testing and a test URL scheme, then verify the destination view exists via accessibility identifiers.
⚠️
WarningDon't put authentication gating inside the router. Let the destination view redirect to login if needed. The router stays dumb; views own their preconditions.
Deep linking is no longer a side quest. With NavigationStack, a codable path, and iOS 18 state restoration, it becomes a first-class architecture concern. Start by extracting your route enum. Make it Hashable, then Codable. Wire the queue. Watch the cold-start flicker disappear.