Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect screen lock/unlock events on the iPhone?

Tags:

How can I detect screen lock/unlock events on the iPhone? When the user unlocks it, I want to show a notification alert from my iPhone app. (For Just like Broadcast Receiver for screen unlock in Android.)

like image 797
Vikas S Singh Avatar asked Oct 25 '11 11:10

Vikas S Singh


People also ask

Can you check unlock history on iPhone?

One of the ways to check iPhone unlock history is through a third-party app. You can download various third-party apps related to iPhone unlock history from the Apple Store.

How do I find screen lock on my iPhone?

On an iPhone with a Home button, press the Home button using the finger you registered with Touch ID. To lock iPhone again, press the side button or Sleep/Wake button (depending on your model). iPhone locks automatically if you don't touch the screen for a minute or so.

How can I get my Lock Screen notifications back when I accidentally unlock my iPhone?

Open the setting app on your iPhone. Select notifications and select the app. Enable persistent notifications.

How do I change what shows on my iPhone Lock Screen?

Lock your device to go to the Lock Screen, then tap and hold on the screen to show the Lock Screen picker (Figure A). Select Customize at the bottom of the first Lock Screen. Select a new wallpaper from the gallery that includes both Apple-selected images and iOS 16 picked images from your Photo Library.


2 Answers

Check this out, I wanted to detect the lock/unlock events, I solved it by Darwin notifications. You can detect the event when the device is locked by "com.apple.springboard.lockcomplete".

//call back
static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    // the "com.apple.springboard.lockcomplete" notification will always come after the "com.apple.springboard.lockstate" notification

    NSString *lockState = (NSString*)name;
    NSLog(@"Darwin notification NAME = %@",name);

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


-(void)registerforDeviceLockNotif
{
    //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);  
}   
like image 85
Rohit Kashyap Avatar answered Oct 07 '22 02:10

Rohit Kashyap


To detect lock/unlock inside app in swift 5 only this worked for me:

override func viewDidLoad() {
    super.viewDidLoad()

     NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.willEnterForegroundNotification, object: nil)
     NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
}

@objc func applicationDidBecomeActive(notification: NSNotification) {
    print("ACTIVE")
}
@objc func applicationDidEnterBackground(notification: NSNotification) {
    print("BACKGROUND")
}
like image 38
Vadim Avatar answered Oct 07 '22 03:10

Vadim