Avoid losing notifications sent by widgets due to spash screen or incorrect controller active

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

In the AppDelegate application:openURL:options: 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 application is open and has MyViewController active, the action is excellent. But, if I am on another view controller in the application or application, it closes, the action is not performed.

Can someone put me on the right track?

NB: My main controller is a UITabBarController with various child view controllers. Some of them are UINavigationControllers (which contain grid controllers), while others are ListViewController .

+7
uiviewcontroller swift nsnotificationcenter appdelegate uilocalnotification
source share
1 answer

The easiest option is to show a view controller that treats it as modal over the tab controller. This is usually the least complicated and the cleanest, as the user can easily be returned to what they were doing before this interaction, when it was done.

If you cannot do this for any reason:

You need to assign some class of responsibility for ensuring that the correct view controller is displayed and the action is indicated on the request when a notification is displayed. It can be a delegate of the application directly, a tab bar controller, or some other specific class that you create and provide a link to the tab controller.

The task is to check the status of the tab controller and, if necessary, show the correct viewing controller, and then inform this view controller in order to start some action.

This class that owns this logic may be the one watching your notification, or you can simply pass the message directly, as your application delegate will likely recognize the instance or create a new instance.

+2
source share

All Articles