Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Controller method for lock button pressed?

Is there an available view controller method that is called when the user presses the lock button? I'm looking for something like viewDidDisappear: or viewWillDisappear:, but specific to the case of the lock button being pressed.

like image 857
Matt Logan Avatar asked Aug 23 '26 19:08

Matt Logan


1 Answers

A notification called UIApplicationDidEnterBackgroundNotification is posted when the user locks their phone. Here's how to listen for it:

In viewDidLoad: of your ViewController:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenLocked) name:UIApplicationDidEnterBackgroundNotification object:nil];

Then, define a method (mine was called screenLocked above) and write code you want to be executed when the screen is locked.

-(void)screenLocked{
    //do stuff
}

Also, to do some necessary cleanup, add this method to your ViewController too.

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}
like image 123
hgwhittle Avatar answered Aug 25 '26 09:08

hgwhittle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!