iOS实现关闭/开启自动锁屏
1 不自动锁屏
[UIApplication sharedApplication].idleTimerDisabled=YES; // 不自动锁屏
2 自动锁屏
[UIApplication sharedApplication].idleTimerDisabled=NO; // 自动锁屏
iOS 检测屏幕状态(是否锁屏)引用 ,收藏一下https://www.jianshu.com/p/fbb3c500853d
1定义宏
//锁屏通知
define NotificationOff CFSTR("com.apple.springboard.lockcomplete")
//解锁通知
define NotificationOn CFSTR("com.apple.springboard.hasBlankedScreen")
2.注册屏幕监听事件
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, ListeningScreenLockState, NotificationOff, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, ListeningScreenLockState, NotificationOn, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
3.监听方法
static void ListeningScreenLockState(CFNotificationCenterRef center,void* observer,CFStringRef name,const void* object,CFDictionaryRef userInfo)
{
NSString* screenState = (__bridge NSString*)name;
if ([screenState isEqualToString:(__bridge NSString*)NotificationOff]) {
NSLog(@"********锁屏**********");
} else {
NSLog(@"********解锁**********");
}
}
二.监听结果 调用方法
由于监听方法为C函数 需要在c函数里面调用iOS 方法
1.将类定义成:
static MinshLivenessViewController *selfClass = nil;
self赋给selfClass
selfClass = self;(我是将这句代码写在viewDidLoad 里面)
3.在c函数里面调用:
[selfClass 方法名];
网友评论