The iOS Mail app launches with your most recently-viewed mailbox already pushed onto the navigation stack, even though it's actually a detail screen and the root view controller is the list of all mailboxes. This would be trivial to do in UIKit, since pushing onto a navigation controller is trivial and could be done, for example, on viewDidLoad or inside of applicationDidFinishLaunching.
NavigationView and NavigationLink are the APIs for performing navigation pushes/pops in SwiftUI, and NavigationLink supports a couple of APIs that mimic programmatic navigation - but I can't quite figure out how I could support this specific use case. The onAppear
closure is called each time the screen appears (more like viewDidAppear
than viewDidLoad
), so setting a selection
value corresponding to a NavigationLink would result in repeatedly pushing that detail screen when the user tries to pop. I could try to do some hacking around maintaining a State
for whether the screen has been freshly viewed or not, but it doesn't seem ideal. (I've also had some issues around NavigationLink's selection
API being flaky, but that's perhaps a separate issue.)
Any suggestions? How might I launch the app onto a detail screen, but otherwise have the NavigationView behave normally without any programmatic intervention?