【问题标题】:iOS device On/Off notificationiOS 设备开/关通知
【发布时间】:2014-07-08 15:40:01
【问题描述】:

我了解此任务需要私有 API,并且该应用不是为 AppStore 设计的。

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

【问题讨论】:

  • "switching to off state" == 你关掉设备还是什么?

标签: ios iphone-privateapi


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    首先,看看这些问题:

    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() 向他们注册;

    【讨论】:

    • 谢谢,但我不需要锁屏事件,而是设备关闭时的事件。
    • 我相信它们是紧密相关的。 com.apple.iokit.hid.displayStatus 和 com.apple.springboard.hasBlankedScreen 也与屏幕打开/关闭直接相关
    • 每次设备锁定时都会发送这些通知。所以他们在这里可能不好。
    • @VictorRonin:我搜索了很多地方,但没有关于 com.apple.mobile.keybagd.lock_status 的详细信息。你能解释一下它到底回调了什么事件吗?谢谢!
    • @nahung89 免责声明:我已经有几年没有接触私有 API 了,而且真的很长时间了(iOS 的 2 个主要版本)。结果,我的知识可能已经过时了。我记得这是一个通知,iOS 将删除 iOS 钥匙串的记忆钥匙。这通常发生在设备关闭后的一段时间。我相信当您解锁设备(输入 PIN 或指纹)时会发生反向(将此密钥放入内存)。如果我没记错的话,“Mac OS X and iOS Internals:”一书中有一些关于此的信息。
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多