user6539552
user6539552

Reputation: 1451

Check if current screen is on or off in iOS

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.

Upvotes: 3

Views: 4024

Answers (3)

Pravin S.
Pravin S.

Reputation: 485

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)
 }

Upvotes: 1

Monica
Monica

Reputation: 17

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")
}

Upvotes: 0

Pravin S.
Pravin S.

Reputation: 485

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);
}

Upvotes: 1

Related Questions