Question

I'm implementing a custom URL scheme in my navigation based app to let users import data from an email, and so I have this method in my app delegate:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

It would be nice, if after importing the data in that method, I could use popToRootViewController to go back to the rootViewController to show the user the new data.

However, that doesn't work if the user had imported the data while a modal view controller or action sheet was up (popToRootViewController doesn't dismiss those, and causes the app to freeze).

Is there any way I can safely dismiss/destroy all view controllers/action sheets/alert views except for the rootViewController?

Or perhaps some way to tell from within my app delegate if the user has an action sheet or modal view controller up?

Or, do I just leave them where they left off, and not provide any immediate indication that data was successfully added?

Was it helpful?

Solution

You'll have to keep track of them yourself. An easy way to do that is to post a notification, with a reference to the modal view/sheet/alert in the userinfo dictionary, every time you display something modal. Whenever a modal view returns, post a different notification.

Observe the notifications in your app delegate (or wherever it makes sense to do so, but the app delegate is simplest). When you receive the first one, store a (weak) reference to the modal view, and zero the reference when you receive the second one. In between, you'll be able to respond to -application:handleOpenURL: by dismissing the modal view if your reference is non-nil.

Note also that in the case of alerts, it's probably best to dismiss them when you enter the background. Weeks might pass before you are foregrounded again, and it's bad style to greet the user with "Are you sure? OK/Cancel" when they may no longer have any idea what you're asking about.

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