iOS开发之应用内检测手机锁屏,解锁状态
iPhone的锁屏监测分为两种方式监听:
1. 程序在前台,这种比较简单。直接使用Darwin层的通知就可以了:
#import <notify.h>
#define NotificationLock CFSTR("com.apple.springboard.lockcomplete")
#define NotificationChange CFSTR("com.apple.springboard.lockstate")
#define NotificationPwdUI CFSTR("com.apple.springboard.hasBlankedScreen")
static void screenLockStateChanged(CFNotificationCenterRef center,void* observer,CFStringRef name,const void* object,CFDictionaryRef userInfo)
{
NSString* lockstate = (__bridge NSString*)name;
if ([lockstate isEqualToString:(__bridge NSString*)NotificationLock]) {
NSLog(@"locked.");
} else {
NSLog(@"lock state changed.");
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationLock, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationChange, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
//setScreenStateCb();
return YES;
}
2. 第二种是程序退后台后,这时再锁屏就收不到上面的那个通知了,需要另外一种方式, 以循环的方式一直来检测是否是锁屏状态,会消耗性能并可能被苹果挂起;
static void setScreenStateCb()
{
uint64_t locked;
__block int token = 0;
notify_register_dispatch("com.apple.springboard.lockstate",&token,dispatch_get_main_queue(),^(int t){
});
notify_get_state(token, &locked);
NSLog(@"%d",(int)locked);
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
while (YES) {
setScreenStateCb();
sleep(1);
}
}
关于 notify.h 请移步到:
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/notify_register_check.3.html
最新文章
- perl学习之路2
- storm基础系列之二----zookeeper的作用
- 详解javascript 存储
- IOS自学
- python中isort的使用
- Activity生命周期(二)
- Android Camera
- android style=";@[package:]style/style_name"; ----------styles.xml
- Android读写JSON格式的数据之JsonWriter和JsonReader
- Linux系统调用过程
- java中进程与线程的区别
- linux磁盘管理增加,扩容
- CDOJ 1962 天才钱vs学霸周2【最大流】
- hive sql 里面的注释方式
- jquery异步ajax超大长度base64图片长字段数据传输问题解决办法和php后台处理办法
- Postgres使用ALTER USER命令修改用户的密码、密码过期,锁定,解锁
- 网络名词拾遗--part2
- 机器学习 python库 介绍
- 【NOI 2018】屠龙勇士(扩欧)
- JVM 体系结构介绍
热门文章
- [51单片机] nRF24L01 无线模块 测试 按键-灯-远程控制
- Backbone事件管理——Backbone.Events模块API结构
- atitit. orm mapping cfg 映射配置(3)-------hbnt one2maney cfg
- node.js cluster多进程、负载均衡和平滑重启
- Java band [Cite]
- bzoj 4300: 绝世好题
- [原创]软件质量保证圈QQ群:197915314
- 使用MFC提供的Http类下载和上传文件
- Serializable 和 Parcelable 区别
- Gabor学习笔记