Logo for tanaschita.com

How to persist navigation state in SwiftUI

Learn how to save and restore NavigationPath to maintain user context across app launches.

21 Apr 2025 · 4 min read

When building multi-screen apps in SwiftUI, preserving where a user left off can improve the user experience. One of the key advantages of SwiftUI's NavigationPath is that it's serializable. As long as all data types in the path conform to Codable, we can save and restore the navigation state.

This allows us to persist the navigation stack across app launches or even sync state between devices.

Let's look at how to do that.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

Making our routes codable

To persist a NavigationPath, every element that goes into it must conform to both Hashable and Codable. If, for example, we used a router based approach to work with NavigationPath, it can look as follows:

enum Route: Hashable, Codable {
case search
case detail(id: String)
}

Encoding and storing navigation state

Once the route type is Codable, we can encode the navigation path and store it using a persistence mechanism such as UserDefaults or the file system.

For example:

// Encode navigation path
if let encodedPath = try? JSONEncoder().encode(navigationPath) {
UserDefaults.standard.set(encodedPath, forKey: "savedNavigation")
}

For larger or sensitive data, we could use the file system or Keychain instead.

Restoring the navigation stack

To restore navigation, we can retrieve the encoded path and decode it back into a NavigationPath:

if let data = UserDefaults.standard.data(forKey: "savedNavigation"),
let decodedPath = try? JSONDecoder().decode(NavigationPath.self, from: data) {
navigationPath = decodedPath
}

We can perform this restoration early in the app lifecycle, for example when initializing a router.

Limitations to keep in mind

While persisting navigation state can be powerful, there are a few important things to consider. First, only types that conform to Codable can be stored in a NavigationPath.

Second, our route definitions must remain stable over time. If we modify the structure of a route, for example by adding or removing enum cases, previously saved navigation data may no longer decode properly.

Finally, we should be mindful of what data we include in our routes. Associated values should be lightweight and safe to persist, especially if using mechanisms like UserDefaults for persistence.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

navigation

swiftui

swift

ios

Using NavigationPath with TabView in SwiftUI

Learn how to integrate NavigationPath with TabView in SwiftUI to manage independent navigation flows.

14 Apr 2025 · 5 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum