2014-07-08 64 views
0

我知道这个任务需要私有API,而且该应用程序不是为AppStore设计的。iOS设备开/关通知

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

+0

“切换到关闭状态”==您关闭设备或什么? –

+0

是的,关闭设备 –

回答

1

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

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

因此,显而易见的解决方案是查看系统正常运行时间。你可以通过这个[NSProcessInfo processInfo].systemUptime获得它。在我的情况下,当设备关闭时我不需要立即知道。我在设备文件系统中的某个文件中定期保存[NSProcessInfo processInfo].systemUptime值和当前日期和时间。当设备打开并启动我的守护进程时,我将文件中的值与当前正常运行时间进行比较。如果当前的正常运行时间较短,则设备关闭并打开。从文件中的日期和时间我知道什么时候设备被关闭。

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()注册到它们;

+0

谢谢,但我不需要锁定屏幕事件,但设备关闭时的事件。 –

+0

我相信他们是紧密相关的。 com.apple.iokit.hid.displayStatus和com.apple.springboard.hasBlankedScreen也与屏幕开启/关闭直接相关 –

+0

每次设备锁定时都会发送这些通知。所以他们在这里可能不太好。 – creker