如何检测 iPhone 上的屏幕锁定/解锁事件?
如何检测 iPhone 上的屏幕锁定/解锁事件?当用户解锁时,我想显示来自我的 iPhone 应用程序的通知警报。 (就像 Android 中用于屏幕解锁的广播接收器一样。)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何检测 iPhone 上的屏幕锁定/解锁事件?当用户解锁时,我想显示来自我的 iPhone 应用程序的通知警报。 (就像 Android 中用于屏幕解锁的广播接收器一样。)
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
看看这个,我想检测锁定/解锁事件,我通过达尔文通知解决了它。
您可以通过
“com.apple.springboard.lockcomplete”
检测设备锁定时的事件。Check this out, I wanted to detect the lock/unlock events, I solved it by Darwin notifications.
You can detect the event when the device is locked by
"com.apple.springboard.lockcomplete"
.要在 swift 5 中检测应用程序内部的锁定/解锁,只有这对我有用:
To detect lock/unlock inside app in swift 5 only this worked for me:
向 App Store 提交应用时,不能使用
com.apple.springboard.lockcomplete
或com.apple.springboard.lockstate
,您的应用将被拒绝因为它是私有 API。com.apple.springboard.lockcomplete
通知并不总是在com.apple.springboard.lockstate
通知之后出现,它可能会提前或稍后发生。您需要设置一个计时器来等待该事件。以下是在 Swift 5 中检测屏幕锁定和解锁状态的方法:
You can't use
com.apple.springboard.lockcomplete
orcom.apple.springboard.lockstate
when submitting your app to App Store, your app will be rejected because it's private API.The
com.apple.springboard.lockcomplete
notification NOT always comes after thecom.apple.springboard.lockstate
notification, it may happen early or later. You need to set a timer to wait for that event.So here is how you can detect screen lock and unlock status, in Swift 5:
可能您需要在 AppDelegate 中实现以下方法:
May be you need to implement following methods in
AppDelegate
:您无法在 iPhone 上执行此操作。
You can't do that on the iPhone.
最初的问题已经很老了,所以对于任何偶然发现这个问题的人来说:
在具有 Face ID 和 Touch ID 的现代设备上,您可以使用 AppDelegate 方法
applicationProtectedDataDidBecomeAvailable(_:)
(docs)和applicationProtectedDataWillBecomeUnavailable(_:)
(文档)。当设备存储被解密或加密时,将调用这些函数。它们的目的是让应用程序知道何时可以读取存储中的数据或何时变得不可用。但是,由于当今几乎所有 iPhone 上都启用了设备加密,因此您还可以使用它们来检测屏幕锁定和解锁事件。
但是,这当然只有在您的应用程序正在运行时才有效。
The original question is quite old, so for anybody who stumbles on this:
On modern devices with Face ID and Touch ID, you can use the AppDelegate methods
applicationProtectedDataDidBecomeAvailable(_:)
(docs) andapplicationProtectedDataWillBecomeUnavailable(_:)
(docs).These are called when the device storage is decrypted or encrypted. They are intended to let the app know when data from the storage can be read or becomes unavailable. However, because device encryption is active on almost any iPhone today, you can also use these to detect screen lock and unlock events.
However, this of course only works as long as your app is actively running.
从当前视图控制器中,您应该为 UIApplicationDidEnterBackgroundNotification 添加一个观察者,并在关闭视图控制器期间删除观察者
[[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
From the current view controller your should add an observer for UIApplicationDidEnterBackgroundNotification and remove the observer during dismissing the view controller
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];