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?
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.
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.
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With