gpt4 book ai didi

ios - 是否可以区分锁定设备和将应用程序发送到后台?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:27:01 24 4
gpt4 key购买 nike

我有一个应用程序需要在使用主页按钮将其发送到后台时执行某些操作,而在使用顶部硬件按钮锁定设备时需要执行其他操作。解决这些要求的标准方法是由 UIApplication 发出的通知和委托(delegate)方法。在 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.

换句话说,很容易区分锁定和后台。在 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 the device
Will enter foreground.
Did become active.

请注意,即使在(解锁)锁定设备时,didEnterBackgroundwillEnterForeground 通知现在也会发出,因此无法区分锁定和后台运行。此更改是否记录在某处?是倒退吗?你知道区分这两种情况的另一种方法吗?

最佳答案

iOS 6

在我通过模拟器进行的初步测试中,使用

检查应用程序状态
[[UIApplication sharedApplication] applicationState]

无论是

- (void)applicationWillEnterForeground:(UIApplication *)application

- (void)applicationDidEnterBackground:(UIApplication *)application

允许您区分锁定设备的调用和只是切换回主屏幕。锁定屏幕将返回 1 (UIApplicationStateInactive),而按下主页按钮将注册为 2 (UIApplicationStateBackground)。

它看起来是一致的,并且应该像在模拟器中一样可靠地在 iOS 设备上运行。

iOS 7

iOS 6 方法在 iOS 7 中不再有效。为了现在执行此操作,您必须使用 CFNotificationCenter 并监听 darwin 通知(标记为:com.apple.springboard.lockcomplete)。您可以在此处找到带有示例项目的 github 存储库:https://github.com/binarydev/ios-home-vs-lock-button

iOS 7 修复的功劳归功于 wqq

关于ios - 是否可以区分锁定设备和将应用程序发送到后台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7896095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com