我有一个应用程序,当使用主页按钮将其发送到后台时需要执行某些操作,而当使用顶部硬件按钮锁定设备时,需要执行其他操作.解决这些要求的标准方法是发出的通知和委托方法UIApplication.在iOS 4上,它们看起来像这样:
// Pressing the home button
Will resign active.
Did enter background.
// Tapping app icon on Springboard
Will enter foreground.
Did become active.
// Pressing the lock button
Will resign active.
// Unlocking the device
Did become active.
Run Code Online (Sandbox Code Playgroud)
换句话说,在锁定和后台之间很容易区分.在iOS 5上,行为发生了变化:
// Pressing the home button
Will resign active.
Did enter background.
// Tapping app icon on Springboard
Will enter foreground.
Did become active.
// Pressing the lock button
Will resign active.
Did enter background.
// Unlocking …Run Code Online (Sandbox Code Playgroud) 我需要做点什么applicationDidEnterBackground.但我需要区分哪个用户操作导致"输入背景":屏幕锁定或按下主页按钮.
我正在使用这个代码,这是从这篇文章 - 如何区分iOS5上的屏幕锁定和主页按钮?:
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");
}
Run Code Online (Sandbox Code Playgroud)
它在iOS6上运行良好.但是在iOS7(设备和模拟器)上UIApplicationStateBackground,无论用户是单击主页还是锁定按钮,我总能得到.
有人知道可能导致这种情况的原因吗?iOS 7更新到多任务后台处理?或者我的应用程序的某些设置(我的应用程序的后台模式已关闭)?
还有替代解决方案吗?