iOS设备开/关通知

时间:2014-07-08 15:40:01

标签: ios iphone-privateapi

我了解此任务需要私有API,并且该应用不适用于AppStore。

那么,是否有可能知道系统(iOS)何时切换到关闭状态以及何时唤醒?也许它会发送一些关于它的系统通知?

2 个答案:

答案 0 :(得分:1)

首先,看看这些问题:

Is there a away to detect the event when iOS device goes to sleep mode (when the screen gets blackened)?

Is there a away to detect the event when iOS device goes to sleep mode (when the screen gets blackened)?

以下是您可能感兴趣的几个私有API:

void SBGetScreenLockStatus (mach_port_t* port, bool *lockStatus, bool *passcodeEnabled);

检查设备是否已锁定且密码受保护。

您可以在此处找到用法:How to find the purple port for the front most application in IOS 5 and above?

此外,当打开/关闭设备时会触发一系列系统范围的通知。

  • com.apple.mobile.keybagd.lock_status
  • com.apple.springboard.lockstate
  • com.apple.iokit.hid.displayStatus
  • com.apple.springboard.hasBlankedScreen
  • com.apple.springboard.lockcomplete

您可以使用CFNotificationCenterAddObserver();

注册给他们

答案 1 :(得分:1)

我假设您正在谈论设备开启/关闭时的通知。

我遇到了同样的问题,通知是我的第一选择。但我发现了更简单,更强大的解决方案。你看,通常只发送一次通知。例如,如果您正在构建一个将侦听这些通知的守护程序,那么您需要确保在发送设备开启通知时它将运行。这就是问题 - 您无法确定并且可能会错过可能对您有所帮助的通知。它对我来说看起来不那么健壮。

如此明显的解决方案是查看系统正常运行时间。您可以使用此[NSProcessInfo processInfo].systemUptime获取它。在我的情况下,我不需要立即知道设备何时关闭。我会定期将[NSProcessInfo processInfo].systemUptime值和当前日期和时间保存在设备文件系统中的某个文件中。当设备打开并启动我的守护程序时,我将文件中的值与当前正常运行时间进行比较。如果当前正常运行时间较短,则设备已关闭并打开。从文件中的日期和时间我知道设备何时关闭。