iOS控制屏幕常亮

//设置屏幕常亮

[UIApplication sharedApplication].idleTimerDisabled = YES;

//取消设置屏幕常亮

[UIApplication sharedApplication].idleTimerDisabled = NO;

但是有些控件的存在是不会锁屏的,比如AVPlayer,播放的时候是不会锁屏的,但是暂停或者停止播放之后系统会自动关闭常亮。

解决方法:

添加一个监听,如果屏幕锁屏被关,立马把它打开,最后移除监听并且关闭锁屏。

//监听锁屏变化

[[UIApplication sharedApplication] addObserver:self forKeyPath:@"idleTimerDisabled"options:NSKeyValueObservingOptionNew context:nil];


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context{

 // setToast(@"值改变操作");

 if(![UIApplication sharedApplication].idleTimerDisabled) {

         [UIApplication sharedApplication].idleTimerDisabled = YES;

    }

}


- (void)dealloc{

 [[UIApplication sharedApplication] removeObserver:self forKeyPath:@"idleTimerDisabled"];

 [UIApplication sharedApplication].idleTimerDisabled = NO;

}

你可能感兴趣的:(iOS控制屏幕常亮)