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.)
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.
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.
Open the setting app on your iPhone. Select notifications and select the app. Enable persistent notifications.
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.
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);
}
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With