Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate between screen lock and home button press on iOS5?

I'm developing an iPhone app that plays audio in the background. I want the audio to keep playing if the user locks the screen, but pause if they decide to switch to another app (by pressing the home button).

On iOS 4 there was no problem because the app would go into the inactive state when the screen was locked and only be moved to the background if the home button was pressed. On iOS 5 when the screen is locked the app is now also moved into the background, so it seems it is no longer possible to tell the difference between the two states. Is there a solution to this problem?

like image 534
Stuart Fisher Avatar asked Nov 28 '11 23:11

Stuart Fisher


People also ask

How to lock iPhone using home button?

It's not possible to use Home button to lock your iPhone. If your iPhone's lock button is broken, using assistive touch is your only resort. You can optionally set up your iPhone to auto-lock. The minimum duration you can set for auto-lock is 30 seconds.

Can I change lock screen buttons?

From the Home screen, tap the Menu Key > Lock screen settings. Tap Shortcuts. You can access the Shortcuts setting only when the lock screen is set to Swipe. Tap one of the icons at the bottom of the screen, then tap the app you want to replace it with.


1 Answers

You can distinguish these two cases by checking the applicationState property of UIApplication. It will be set to UIApplicationStateInactive for an application that did enter background because of the lock screen, and to UIApplicationStateBackground otherwise.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateInactive) {
        NSLog(@"Sent to background by locking screen");
    } else if (state == UIApplicationStateBackground) {
        NSLog(@"Sent to background by home button/switching to other app");
    } 
}
like image 105
Tammo Freese Avatar answered Oct 13 '22 00:10

Tammo Freese