2016-09-29 44 views
1

如何檢測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) 
    } 
+0

達爾文通知是否在應用程序處於前臺時起作用? – OhadM

+0

此代碼無法使用。 – fmashkoor

+0

首先,你的方法displayStatusChanged假設是靜態的。請檢查。 – OhadM

回答

0

據我們所知,你不能使用本地代碼檢測鎖屏解鎖狀態。通過使用私人API,將有機會檢測屏幕鎖定解鎖狀態。如果您使用Apple的私人API,您的應用可能會被拒絕。我們建議不要使用蘋果私人API。

,如果你想在監獄裏破碎設備屏幕鎖解鎖事件時,可以找到下面的鏈接答案

Getting state for system wide notifications in iOS and OS X

Lock Unlock events iphone

Detect screen on/off from iOS service

4

有代碼中的幾個誤區樣品:

  • 在swift中使用CFString由簡單的演員完成:myString as CFString,不再有CFSTR() ...
  • 獲得通知回叫的最簡單方法是添加使用Unmanaged.passUnretained(self).toOpaque()的觀察者。這會給你趕上回調類

最終的可能性,迅速的版本是Objective-C的一個,在這裏斯威夫特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") 
    } 
} 

,以防萬一,請不要忘記刪除您的觀察者:

CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), 
            Unmanaged.passUnretained(self).toOpaque(), 
            nil, 
            nil) 
+0

這適用於我,但是當我解鎖iPhone時,我會收到消息「DEVICE UNLOCKED」兩次。設備鎖定狀態比「鎖定」和「解鎖」更多嗎? –

+0

@NikitaSavchuk你能解決雙重信息的問題,你收到兩次「DEVICE UNLOCKED」? – Tomasero

+0

@Tomasero悲傷但沒有 –