一.实现监听
1.定义宏
//锁屏通知
#define NotificationOff CFSTR("com.apple.springboard.lockcomplete")
//解锁通知
#define NotificationOn CFSTR("com.apple.springboard.hasBlankedScreen")
2.注册屏幕监听事件
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, ListeningScreenLockState, NotificationOff, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, ListeningScreenLockState, NotificationOn, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
3.监听方法
static void ListeningScreenLockState(CFNotificationCenterRef center,void* observer,CFStringRef name,const void* object,CFDictionaryRef userInfo)
{
NSString* screenState = (__bridge NSString*)name;
if ([screenState isEqualToString:(__bridge NSString*)NotificationOff]) {
NSLog(@"********锁屏**********");
} else {
NSLog(@"********解锁**********");
}
}
二.监听结果 调用方法
由于监听方法为C函数 需要在c函数里面调用iOS 方法
1.将类定义成:
static MinshLivenessViewController *selfClass = nil;
- self赋给selfClass
selfClass = self;(我是将这句代码写在viewDidLoad 里面)
3.在c函数里面调用:
[selfClass 方法名];
网友评论