问题描述
如何在iPhone上检测屏幕解锁事件?当用户解锁时,我想在我的应用中执行操作。我搜索谷歌搜索但只找到与目标C相关的代码,将其更改为快速但不起作用。
关注此博客:
。
任何帮助如何在swift中检测到它。
下面是代码更改为swift ..
How can i detect screen unlock events on iPhone? When the user unlocks it, I want to perform an action in my app. I searched on googled but only found code related to objective C , change it to swift but its not working.
Follow this blog:http://kidtechblogs.blogspot.com/2014/07/how-to-detect-screen-lockunlock-events.html.
Any help how can i detect it in swift.Below is the code change into swift..
func displayStatusChanged(center: CFNotificationCenter, observer: Void, name: CFString, object: Void, userInfo: CFDictionaryRef) {
// the "com.apple.springboard.lockcomplete" notification will always come after the "com.apple.springboard.lockstate" notification
let lockState = (name as String)
print("Darwin notification NAME = \(name)")
if (lockState == "com.apple.springboard.lockcomplete") {
print("DEVICE LOCKED")
}
else {
print("LOCK STATUS CHANGED")
}
}
func registerforDeviceLockNotification() {
//Screen lock notifications
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
nil, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockcomplete"), // event name
nil, // object
.deliverImmediately)
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
nil, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockstate"), // event name
nil, // object
.deliverImmediately)
}
推荐答案
代码示例中有一些错误:
There a few errors in your code sample:
- 在swift中使用
CFString
是通过一个简单的演员来完成的:myString as CFString
,不再CFSTR()
... - 最简单的方法获取通知回调是使用
Unmanaged.passUnretained(self).toOpaque()
添加观察者。这将使你有可能在你的班级中捕捉回调
- Using
CFString
in swift is done by a simple cast:myString as CFString
, no moreCFSTR()
... - The easiest way to get the notification callback is to have add an observer using
Unmanaged.passUnretained(self).toOpaque()
. That will give you the possibility to catch the callback in your class
最后,swift版本与目标完全不同 - c one,这里是Swift 3中的完整代码:
In the end, the swift version is quite different from the objective-c one, here the full code in Swift 3:
func registerforDeviceLockNotification() {
//Screen lock notifications
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
Unmanaged.passUnretained(self).toOpaque(), // observer
displayStatusChangedCallback, // callback
"com.apple.springboard.lockcomplete" as CFString, // event name
nil, // object
.deliverImmediately)
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
Unmanaged.passUnretained(self).toOpaque(), // observer
displayStatusChangedCallback, // callback
"com.apple.springboard.lockstate" as CFString, // event name
nil, // object
.deliverImmediately)
}
private let displayStatusChangedCallback: CFNotificationCallback = { _, cfObserver, cfName, _, _ in
guard let lockState = cfName?.rawValue as? String else {
return
}
let catcher = Unmanaged<MyClassObserving>.fromOpaque(UnsafeRawPointer(OpaquePointer(cfObserver)!)).takeUnretainedValue()
catcher.displayStatusChanged(lockState)
}
private func displayStatusChanged(_ lockState: String) {
// the "com.apple.springboard.lockcomplete" notification will always come after the "com.apple.springboard.lockstate" notification
print("Darwin notification NAME = \(lockState)")
if (lockState == "com.apple.springboard.lockcomplete") {
print("DEVICE LOCKED")
} else {
print("LOCK STATUS CHANGED")
}
}
以防万一,不要忘记删除你的观察者:
and just in case, don't forget to remove your observer:
CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(),
Unmanaged.passUnretained(self).toOpaque(),
nil,
nil)
这篇关于在IOS Swift中检测屏幕解锁事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!