一.實(shí)現(xiàn)監(jiān)聽
1.定義宏
//鎖屏通知
#define NotificationOff CFSTR("com.apple.springboard.lockcomplete")
//解鎖通知
#define NotificationOn CFSTR("com.apple.springboard.hasBlankedScreen")
2.注冊屏幕監(jiān)聽事件
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, ListeningScreenLockState, NotificationOff, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, ListeningScreenLockState, NotificationOn, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
3.監(jiān)聽方法
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(@"********解鎖**********");
}
}
二.監(jiān)聽結(jié)果 調(diào)用方法
由于監(jiān)聽方法為C函數(shù) 需要在c函數(shù)里面調(diào)用iOS 方法
1.將類定義成:
static MinshLivenessViewController *selfClass = nil;
- self賦給selfClass
selfClass = self;(我是將這句代碼寫在viewDidLoad 里面)
3.在c函數(shù)里面調(diào)用:
[selfClass 方法名];