I am creating an application with swift. I have multiple viewControllers. When Someone in a particular viewController and press the home button, then I want to dismiss that ViewController. I don't want any action in other view controllers. I found out when I press the home button following Appdelagate func will call
func applicationDidEnterBackground(application: UIApplication) {
print("Enter my Guess")
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
So I manage to capture this action using Notifier on my view controller (viewDidLoad) like the following
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("myObserverMethod:"), name:UIApplicationDidEnterBackgroundNotification, object: nil)
And
func myObserverMethod(notification : NSNotification) {
if let viewControllers = navigationController?.viewControllers {
for viewController in viewControllers {
// some process
if viewController.isKindOfClass(HomeVC) {
print("I am calledr")
// dismissViewControllerAnimated(false, completion: nil)
}
}
}
}
Now my problem View controller not dismissing. I mean no action happen. How to handle this.
Note : Whenever I press the home button myObserverMethod func called and print("I am calledr") also called.
What I am doing wrong?
Edit 1: I want to dismiss if I am in HomeVC(View controller name) otherwise no need to dismiss the view controller.
Edit 2: I found out why it always enters to that if.. Condition. The reason is always it keeps HomeVC in the memory. So always enters into that if. So I add another one condition inside of that like the following.
if (viewController.isViewLoaded() && viewController.view.window != nil){
print("I am calledr")
viewController.dismissViewControllerAnimated(false, completion: nil)
// self.presentingViewController!.dismissViewControllerAnimated(false, completion: nil)
// viewController.dismissViewControllerAnimated(false, completion: nil)
}
No no action happened. I mean the view controller not dismissed.