I try to do something when the home button and power button has been clicked. I am developing in iOS.
This is the code I use:
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"viewDidDisappear");
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"viewWillDisappear");
}
- (void)applicationFinishedRestoringState{
NSLog(@"applicationFinishedRestoringState");
}
Why is the above function not being called when I click the power button or home button on the iPhone?
Did I miss something?
viewDidDisappear:
and viewWillDisappear:
will get called if the view is pushed or popped or in anyway gets disappeared in your own runloop, going to background by pressing home or power button doesn't count as view' related events, but rather app related events. you should register for UIApplicationWillResignActiveNotification
notification instead.
e.g.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disappearSelector) name:UIApplicationWillResignActiveNotification object:nil];