iOS 锁屏和息屏监听

锁屏和息屏监听

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   //传感器(红外感应)打开
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
    //设置监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(sensorStateChange:)
                                                     name:UIDeviceProximityStateDidChangeNotification
                                                   object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(LOCK_SCREEN:)
                                                 name:UIApplicationProtectedDataWillBecomeUnavailable
                                               object:nil];
     
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(UN_LOCK_SCREEN:)
                                                  name:UIApplicationProtectedDataDidBecomeAvailable
                                                object:nil];
    
}
- (void)LOCK_SCREEN:(NSNotificationCenter *)notification {
    NSLog(@">>>锁屏");
}
- (void)UN_LOCK_SCREEN:(NSNotificationCenter *)notification {
    NSLog(@">>>解除锁屏");
}
- (void)sensorStateChange:(NSNotificationCenter *)notification {
    if ([[UIDevice currentDevice] proximityState] == YES) {
        NSLog(@">>>屏幕熄灭");
    }else{
        NSLog(@">>>屏幕亮起");
    }
}

一些说明
如下方法是监听 传感器(红外感应) YES开启 NO关闭
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];

这种息屏监听是监听的传感器,手机放置不动直至黑屏,实际上是手机锁屏,注意甄别。

iOS App不息屏设置

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容