app delegate viewcontroller unwind

When I receive a request to open the app directly to a certain customViewController, I am able to handle that request within AppDelegate and open direct to that viewController like so:


let storyBoard = UIStoryboard(name: "Main", bundle: nil)
guard let controller = storyBoard.instantiateViewController(withIdentifier: "CustomViewController") as? CustomViewController else {
       return
 }

self.window?.rootViewController = controller

This opens my custom view directly as I want, and I can segue from there forward, but the unwind segues back aren’t firing, I assume they’re not being created due to the way the AppDelegate makes this view the rootViewController, bypassing other parts of my storyboard's structure.


What is the better way here to start my app from AppDelegate so that the view hierarchy and segues are all set as they would be in a typical app opening instance?


In normal navigation, there are two show segues that are performed on the way to my customViewController from the initial Storyboard entry point viewController.


Any advice about how to accomplish this directly from AppDelegate but then still allow the user to navigate freely around the app using the segues/unwindsegues built in to the storyboard would be greatly appreaciated! Thanks so much in advance!

Accepted Answer

Could you try something like this :


- create a global

var openOnRequest = false


- Set to true here:


let storyBoard = UIStoryboard(name: "Main", bundle: nil)
//     Next statement to change
guard let controller = storyBoard.instantiateViewController(withIdentifier: "CustomViewController") as? CustomViewController else {
       return
}
openOnRequest = true     // That would be the obly place where it could be set true
self.window?.rootViewController = controller


- but change the controller you instantiate, to instantiate the "root" one


- In this root, on viewDidload, test if openOnRequest is true

- if so, trigger the segue to CustomViewController


I think doing so, all views will be stacked properly and you will be able to unwind.


Does it make sense to you ?

performSegue can't be called in viewDidLoad, but in viewDidAppear it works!

I had to create some new segues and unwinds specific to my Entry Point View <-> CustomView but I've now got it working well.


Thank you so much for chiming in with a suggestion and getting me to think about the situation a different way!

app delegate viewcontroller unwind
 
 
Q