Question

EDIT: One question left at bottom - how to ensure I don't call methods before any VC presented by a navigation controller is properly finished being popped.

EDIT: An useful tutorial in doing pretty much what I want, but not quite.

So I have this setup

Initial VC -> Tab controller -> (three VC's, one of them...) NavController containing file list -> File editor

At any time, I might get a application:handleOpenUrl message sent to my app delegate that tells me that the user wants to edit a file, for example an attachment from an email. Depending on the state of my app, all or some of these VC's might be loaded. In order to react sensibly to the URL, I need for the NavController to be loaded at the very least.

I suppose the real objective is to get the app in a state where the NavController is loaded, and the currently active controller, so I can send it a message from the AppDelegate to load a given file once it's processed.

So: What is best practice here?

EDIT

So I played around a bit, and got this far:

I know that the root VC is a UITabBarController, and is always loaded.

I know that my NavigationController is at index 2

I know that it either has loaded the NC, or will do so on demand if I switch to the tab

So the algorithm is then: Grab the root tabbar VC. Tell it to switch to tab 2 if not there already. Grab the NavigationController at tab 2. Pop it to root, and tell it what to do.

Now my only issue is that in this code, the action that I invoke on the root VC of the Navigation controller is performed BEFORE any VC currently presented by the NC has a change to do viewWillDisappear.

UITabBarController *root = (UITabBarController*) self.window.rootViewController;
if (root.selectedIndex != 2) {
    root.selectedIndex = 2;
};
UINavigationController *nc = root.viewControllers[2];
[nc popToRootViewControllerAnimated:YES];
NSObject<SGPTFileManagerViewProtocol> *fileManager = (NSObject<SGPTFileManagerViewProtocol> *) nc.visibleViewController;

// Whoops! This method is invoked before the navigation controller is done popping a presented view controller!
[fileManager METHOD-FOR-HANDLING-THE-URL-HERE];
Was it helpful?

Solution

Well, it turns out it's a bit tricky, and there are no off the rack solutions.

The reason it's tricky is simple: Since there's no way for any outsider to know how you want your app state to respond to an opened URL, there's no way to do a framework. You are left with the basic tools of introspection, traversing the view hierarchy etc.

A useful tip: You can actually debug the process of opening an URL on the device. Go to the debug profile, and set it to wait for the app to be manually started. You can no go to your email or whereever and invoke the link, and the debugger will kick in as your app starts.

In XCode 5: Product -> Scheme -> Edit scheme (scheme for running) select Launch option "Wait for to be launched manually

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top