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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With