Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a prettier way of telling apart task switching and screen locking-originated <code>applicationWillResignActive:</code> callbacks which doesn't even involve undocumented features such as the accelerometer state.</p> <p>When the app is moving to the background, the app delegate is first sent an <code>applicationWillResignActive:</code>, then an <code>applicationDidEnterBackground:</code>. When the app is interrupted by pressing the Lock button or by an incoming phone call, the latter method is not called. We can use this information to distinguish between the two scenarios.</p> <p>Say you want to be called back in the <code>screenLockActivated</code> method if the screen gets locked. Here's the magic:</p> <pre><code>- (void)applicationWillResignActive:(UIApplication*)aApplication { [self performSelector:@selector(screenLockActivated) withObject:nil afterDelay:0]; } - (void)applicationDidEnterBackground:(UIApplication*)aApplication { [NSObject cancelPreviousPerformRequestsWithTarget:self]; } - (void)screenLockActivated { NSLog(@"yaay"); } </code></pre> <p>Explanation:</p> <p>By default, we assume that every call to <code>applicationWillResignActive:</code> is because of an active->inactive state transition (as when locking the screen) but we generously let the system prove the contrary within a timeout (in this case, a single runloop cycle) by delaying the call to <code>screenLockActivated</code>. In case the screen gets locked, the system finishes the current runloop cycle without touching any other delegate methods. If, however, this is an active->background state transition, it also invokes <code>applicationDidEnterBackground:</code> before the end of the cycle, which allows us to simply cancel the previously scheduled request from there, thus preventing it from being called when it's not supposed to.</p> <p>Enjoy!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload