How to differentiate between screen lock and home button press on iOS5?(如何区分iOS5上的屏幕锁定和主页按钮按下?)
问题描述
我正在开发一个在后台播放音频的 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?
推荐答案
您可以通过检查 UIApplication
的 applicationState
属性来区分这两种情况.对于因锁屏而进入后台的应用程序,它将设置为 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上的屏幕锁定和主页按钮按下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何区分iOS5上的屏幕锁定和主页按钮按下?


基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01