Skip to main content

How to save and load NavigationStack paths using Codable

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to save and load NavigationStack paths using Codable 관련

SwiftUI by Example

Back to Home

How to save and load NavigationStack paths using Codable | SwiftUI by Example

How to save and load NavigationStack paths using Codable

Updated for Xcode 15

New in iOS 16

When managing SwiftUI's NavigationStack path using a NavigationPath object, we can save and load our whole path using Codable – we can store the complete navigation stack and restore it later, so the user comes back to the app exactly where they left it.

This is best handled by wrapping up your storage in a separate ObservableObject class, which can take the responsibility of loading and saving path data away from your views. For example, this class loads a saved when it's created, and saves the path whenever its NavigationPath property gets changed:

class PathStore: ObservableObject {
    @Published var path = NavigationPath() {
        didSet {
            save()
        }
    }

    private let savePath = URL.documentsDirectory.appending(path: "SavedPathStore")

    init() {
        if let data = try? Data(contentsOf: savePath) {
            if let decoded = try? JSONDecoder().decode(NavigationPath.CodableRepresentation.self, from: data) {
                path = NavigationPath(decoded)
                return
            }
        }
    }

    func save() {
        guard let representation = path.codable else { return }

        do {
            let data = try JSONEncoder().encode(representation)
            try data.write(to: savePath)
        } catch {
            print("Failed to save navigation data")
        }
    }
}

That's a neatly reusable class that you can put to work immediately – as long as the data you write into NavigationPath conforms to Codable, it will work.

For example, we could create a simple detail view capable of showing a number the user selected while also allowing them to navigate deeper by selecting another number, then use that with our PathStore class so that navigation is automatically loaded and saved:

struct DetailView: View {
    var id: Int

    var body: some View {
        VStack {
            Text("View \(id)")
                .font(.largeTitle)
            NavigationLink("Jump to random", value: Int.random(in: 1...100))
        }
    }
}

struct ContentView: View {
    @StateObject private var pathStore = PathStore()

    var body: some View {
        NavigationStack(path: $pathStore.path) {
            DetailView(id: 0)
                .navigationDestination(for: Int.self, destination: DetailView.init)
                .navigationTitle("Navigation")
        }
    }
}

If you run that code, you'll see you can navigate through as many levels of DetailView as you want, and your data will automatically be stored – you can quit the app and return just fine, and your navigation history will remain intact.

Similar solutions…
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks
All SwiftUI property wrappers explained and compared | SwiftUI by Example

All SwiftUI property wrappers explained and compared
How to push a new view onto a NavigationStack | SwiftUI by Example

How to push a new view onto a NavigationStack
Building a menu using List | SwiftUI by Example

Building a menu using List
How to use Instruments to profile your SwiftUI code and identify slow layouts | SwiftUI by Example

How to use Instruments to profile your SwiftUI code and identify slow layouts

이찬희 (MarkiiimarK)
Never Stop Learning.