我正在开发一款在后台播放音频的iPhone应用。如果用户锁定屏幕,我希望音频继续播放,但如果他们决定切换到另一个应用程序(通过按主页按钮),则暂停。
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).
在iOS 4上没有问题,因为当屏幕被锁定时应用程序将进入非活动状态,并且只有在按下主页按钮时才会移动到后台。在iOS 5上,当屏幕被锁定时,应用程序现在也被移动到后台,因此似乎不再可能分辨出这两种状态之间的区别。是否有解决此问题的方法?
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?
推荐答案您可以通过检查 applicationState UIApplication 的属性。对于因锁定屏幕而输入后台的应用程序,它将设置为 UIApplicationStateInactive ,否则设置为 UIApplicationStateBackground 。
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"); } }更多推荐
如何在iOS5上区分屏幕锁定和主页按钮按下?
发布评论