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];

你可能感兴趣的:(iOS 锁屏和息屏监听)