Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid losing notifications sent by a widget because of a spash screen or wrong controller active

I have an widget that calls its corresponding app through NSURL and extensionContext to activate a specific action in the app.

In AppDelegate's application:openURL:options: method I have:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    if let path = url.path{
        if path.containsString("action"){
          NSNotificationCenter.defaultCenter().postNotificationName(MyViewController.purchasmyActionKey, object: nil)
        }
    }
    return true
}

When the app is open, and has MyViewController active, the action is executed perfectly. But, if I am on another view controller in the app or the app is closed, the action is not executed.

Can somebody set me on the right track?

NB: My main controller is a UITabBarController with various child view controllers. Some are UINavigationControllers (which contain grid controllers) and the other one is a ListViewController.

like image 373
tolbard Avatar asked Aug 25 '16 11:08

tolbard


1 Answers

The simplest option is to show your view controller which handles this as a modal over the tab controller. This is generally the least complex and the cleanest as the user can be easily returned to what they were doing before this interaction when they're done.

If you can't do that for some reason:

You need to designate some class the responsibility of ensuring that the correct view controller is shown and told to action the request when the notification is seen. That could be the app delegate directly, the tab bar controller or some other specific class that you create and provide with a reference to the tab controller.

It's job is to check the state of the tab controller and show the correct view controller if required, then tell that view controller to begin some action.

This class who owns this logic could be the one that observes your notification, or you could just pass the message directly as your app delegate will likely know the instance or be creating a new instance.

like image 89
Wain Avatar answered Sep 18 '22 23:09

Wain