代碼設(shè)置屏幕常亮:
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
一句代碼搞定!
補(bǔ)充:
APP某個(gè)版本上線后有司機(jī)反饋應(yīng)用升級(jí)后如果不操作APP僧鲁,屏幕不久就會(huì)息屏嘿歌。對(duì)于司機(jī)來(lái)說(shuō)在行駛過(guò)程中黑屏或者駕駛中操作手機(jī)都是一種危險(xiǎn)行為哪雕,而技術(shù)人員也沒(méi)辦法通知所有的用戶在設(shè)置-顯示與亮度-自動(dòng)鎖定選擇永不誊垢,所以直接在代碼層面解決該問(wèn)題才是王道(被動(dòng)等待司機(jī)反饋掉弛,不如主動(dòng)出擊)。
經(jīng)網(wǎng)上查詢得知在調(diào)用系統(tǒng)拍照結(jié)束時(shí)系統(tǒng)會(huì)將idleTimerDisabled屬性設(shè)置為NO(也有文章介紹在使用AVPlayer播放結(jié)束后該屬性也會(huì)被設(shè)置為NO喂走,然而經(jīng)過(guò)在iOS13.3系統(tǒng)的iPhone XS Max上測(cè)試發(fā)現(xiàn)屬性值并未改變)。受此啟發(fā)谋作,可能新版本中某功能也會(huì)存在類似設(shè)置芋肠。
解決方法:監(jiān)聽(tīng)idleTimerDisabled屬性,當(dāng)值被設(shè)置為NO時(shí)遵蚜,將idleTimerDisabled屬性值修改為YES帖池。同時(shí)不要忘記在合適的地方移除監(jiān)聽(tīng)。
代碼如下:
- (void)keepIdleTimerDisabled {
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
[[UIApplication sharedApplication] addObserver:self forKeyPath:@"idleTimerDisabled" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if (![UIApplication sharedApplication].idleTimerDisabled) {
[UIApplication sharedApplication].idleTimerDisabled = YES;
}
}
- (void)dealloc{
[[UIApplication sharedApplication] removeObserver:self forKeyPath:@"idleTimerDisabled"];
[UIApplication sharedApplication].idleTimerDisabled = NO;
}