美文网首页
iOS 检测屏幕状态(是否锁屏)

iOS 检测屏幕状态(是否锁屏)

作者: 梦月落花LOVE | 来源:发表于2017-08-09 18:25 被阅读372次

一.实现监听

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;
  1. self赋给selfClass
selfClass = self;(我是将这句代码写在viewDidLoad 里面)

3.在c函数里面调用:

[selfClass 方法名];

相关文章

网友评论

      本文标题:iOS 检测屏幕状态(是否锁屏)

      本文链接:https://www.haomeiwen.com/subject/gopqrxtx.html