如何檢測iPhone上的屏幕解鎖事件?當用戶解鎖它時,我想在我的應用程序中執行一個操作。我搜索了谷歌搜索,但只找到與目標C相關的代碼,將其更改爲快捷但不工作。
關注此博客: http://kidtechblogs.blogspot.com/2014/07/how-to-detect-screen-lockunlock-events.html。
任何幫助如何檢測到它在迅速。 下面是代碼改變進入迅捷..檢測IOS中的屏幕解鎖事件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)
}
達爾文通知是否在應用程序處於前臺時起作用? – OhadM
此代碼無法使用。 – fmashkoor
首先,你的方法displayStatusChanged假設是靜態的。請檢查。 – OhadM