如何检测iPhone上的屏幕locking/解锁事件?

如何检测iPhone上的屏幕locking/解锁事件? 当用户解锁它,我想显示从我的iPhone应用程序通知警报。 (就像广播接收机在Android中用于屏幕解锁一样。)

其实我想如果我退出应用程序并lockingiPhone,一段时间后我已经解锁iPhone,然后退出应用程序显示通知或提醒启动应用程序。

你不能在iPhone上这样做。

检查了这一点,我想检测locking/解锁事件,我解决了达尔文通知。 当设备被"com.apple.springboard.lockcomplete"locking时,您可以检测到事件。

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

也许你需要在AppDelegate实现以下方法:

告诉代performance在应用程序在后台。

 - (void)applicationDidEnterBackground:(UIApplication *)application 

告诉代表该应用程序已经激活。

 - (void)applicationDidBecomeActive:(UIApplication *)application 

告诉代表该应用程序即将变为非活动状态。

 - (void)applicationWillResignActive:(UIApplication *)application 

从当前的视图控制器,你应该添加一个UIApplicationDidEnterBackgroundNotification的观察者,并在解散视图控制器时删除观察者[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];