您当前的位置: 首页 >  ios

柳鲲鹏

暂无认证

  • 0浏览

    0关注

    4642博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

iOS 监听锁屏/解锁事件

柳鲲鹏 发布时间:2018-06-13 11:45:40 ,浏览量:0

必须使用新接口,才能审核通过:

applicationProtectedDataWillBecomeUnavailable

applicationProtectedDataDidBecomeAvailable

 

以下内容已过时,请务必注意!

//监听锁屏事件

#define kNotificationLock CFSTR("com.apple.springboard.lockcomplete")

//监听屏幕状态变化事件

#define kNotificationChange CFSTR("com.apple.springboard.lockstate")

didFinishLaunchingWithOptions()中监听:

//监听锁屏事件  

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, kNotificationLock, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);  

//监听屏幕状态变化事件  

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, kNotificationChange, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);

 

定义一个全局变量stateChaned,用来判断屏幕变化的状态

@property (nonatomic, assign)BOOL stateChanged;

定义AppDelegate的单例,以便在回调函数中调用stateChanged

//定义AppDelegate单例
+ (AppDelegate *)sharedDelegate{
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
    
}
实现通知回调函数:
static void screenLockStateChanged(CFNotificationCenterRef center,void* observer,CFStringRef name,const void* object,CFDictionaryRef userInfo){
    
    NSString* lockstate = (__bridge NSString*)name;
    if ([lockstate isEqualToString:(__bridge  NSString*)kNotificationLock]) {
        [AppDelegate sharedDelegate].stateChanged = YES;
        NSLog(@"锁屏");
    }
    else{
        if ([AppDelegate sharedDelegate].stateChanged) {
            [AppDelegate sharedDelegate].stateChanged = NO;
        }else{
            NSLog(@"解锁");
        }
    }
    
}

 

 

关注
打赏
1665724893
查看更多评论
立即登录/注册

微信扫码登录

0.0586s