3

I know that it is possible to register an event listener when there is screen on/off event. What if I want to check whether currently the screen is on or off? Any method for me to check it?

If I use notification to check, here is the event that will happen:

When I lock the screen. It will trigger

--- received notification: com.apple.springboard.hasBlankedScreen --- received notification: com.apple.springboard.lockcomplete --- received notification: com.apple.springboard.lockstate --- received notification: com.apple.iokit.hid.displayStatus

When I unlock the screen, it will trigger

--- received notification: com.apple.springboard.hasBlankedScreen --- received notification: com.apple.springboard.lockstate --- received notification: com.apple.iokit.hid.displayStatus

I cannot simply detect lockcomplete to see if it is currently off, because it will also trigger lockstate and displaystatus when i tried to lock the screen.

user6539552
  • 1,331
  • 2
  • 19
  • 39
  • see this : http://stackoverflow.com/a/14208787/3901620 – KKRocks May 19 '17 at 09:45
  • Yes, i know this one, and this one only trigger if there is on / off action by the user. Let say my code running continuously, even at the background, i want to know if there is any method to know if currently the screen is on or off, just a true / false condition – user6539552 May 19 '17 at 09:47

3 Answers3

1

Try with :

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    CFStringRef nameCFString = (CFStringRef)name;
    NSString *lockState = (NSString*)nameCFString;
    NSLog(@"Darwin notification NAME = %@",name);

    if([lockState isEqualToString:@"com.apple.springboard.lockcomplete"])
    {
        NSLog(@"DEVICE LOCKED");
    }
    else
    {
        NSLog(@"LOCK STATUS CHANGED");
    }
}

-(void)registerforDeviceLockNotification
{
    //Screen lock notifications
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                    NULL, // observer
                                    displayStatusChanged, // callback
                                    CFSTR("com.apple.springboard.lockcomplete"), // event name
                                    NULL, // object
                                    CFNotificationSuspensionBehaviorDeliverImmediately);

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                    NULL, // observer
                                    displayStatusChanged, // callback
                                    CFSTR("com.apple.springboard.lockstate"), // event name
                                    NULL, // object
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
}
Pravin S.
  • 465
  • 7
  • 22
1

Here is the simple solution:

Place the code below in viewDidLoad

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.applicationDidBecomeActive(notification:)), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.applicationDidEnterBackground(notification:)), 
        name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

These methods gets called when the device is locked or unlocked.

@objc func applicationDidBecomeActive(notification: NSNotification) {
    print("Device is unlocked")
}

@objc func applicationDidEnterBackground(notification: NSNotification) {
        print("Device is locked")
}
shim
  • 9,289
  • 12
  • 69
  • 108
Monica
  • 19
  • 6
  • 3
    it may not matter for most people, but just to make this more clear for the readers, this is checking whether the app is in the foreground or not (for example, the screen could be unlocked and the app minimized and this method would think the screen is locked) – quemeful Jan 01 '20 at 18:38
0
private func displayStatusChanged(center: CFNotificationCenterRef?, observer: UnsafeMutableRawPointer?, name: CFString?, object: UnsafeRawPointer?, userInfo: CFDictionaryRef?) {
let nameCFString = name
let lockState = nameCFString as String
if let aName = name {
    print("Darwin notification NAME = \(aName)")
}
if (lockState == "com.apple.springboard.lockcomplete") {
    print("DEVICE LOCKED")
} else {
    print("LOCK STATUS CHANGED")
}
}  
func registerforDeviceLockNotification() {
  CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),  /*center */nil,  /* observer */displayStatusChanged,  /* callback */"com.apple.springboard.lockcomplete",  /* event name */nil,  /* object */CFNotificationSuspensionBehavior.deliverImmediately)
  CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),  /*center */nil,  /* observer */displayStatusChanged,  /* callback */"com.apple.springboard.lockstate",  /* event name */nil,  /* object */CFNotificationSuspensionBehavior.deliverImmediately)
 }
Pravin S.
  • 465
  • 7
  • 22