2016-09-29 2 views
1

iPhone에서 화면 잠금 해제 이벤트를 감지하려면 어떻게해야합니까? 사용자가 잠금을 해제하면 내 앱에서 작업을 수행하려고합니다. 내가 검색 좀하지만 객관적인 C와 관련된 코드를 발견, 신속하지만 그 작동하지 않는 것으로 변경했습니다.
이 블로그를 따르십시오 : http://kidtechblogs.blogspot.com/2014/07/how-to-detect-screen-lockunlock-events.html.
어떻게 도와 드릴까요? 우리는 당신이 네이티브 코드를 사용하여 화면 잠금 - 해제 상태를 감지 할 수 없습니다 알고 다음은 지금까지 ..IOS 스위프트에서 화면 잠금 해제 이벤트 감지

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

SWIFT에 코드를 변경합니다. 개인 API를 사용하면 화면 잠금 - 잠금 해제 상태를 감지 할 수 있습니다. 사과의 개인 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

이 코드에서 몇 가지 오류 샘플 :

  • CFString을 신속하게 사용하는 것은 간단한 캐스트로 수행됩니다. , 더 이상 CFSTR() ...
  • 통지 콜백을 얻는 가장 쉬운 방법은 Unmanaged.passUnretained(self).toOpaque()을 사용하는 옵저버를 추가하는 것입니다.

    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") 
        } 
    } 
    
    : 그것은 당신에게 당신의 클래스 결국

에 콜백을 잡을 수있는 가능성을 줄 것이다, 신속한 버전은 여기에 목표는 -c 하나, 스위프트 3의 전체 코드는 매우 다르다

과 같은 경우에는 반드시 관찰자를 삭제해야합니다.

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

이 기능은 나를 위해 작동하지만 iPhone을 잠금 해제 할 때 "DEVICE UNLOCKED"메시지가 두 번 울립니다. "잠금"및 "잠금 해제"보다 더 많은 장치 잠금 상태가 있습니까? –

+0

@NikitaSavchuk님께 서 "DEVICE UNLOCKED"를 두 번 받았다면 이중 메시지 문제를 해결할 수 있었습니까? – Tomasero

+0

@Tomasero 슬프게도 아니지만 –

관련 문제