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

最新文章

  1. perl学习之路2
  2. storm基础系列之二----zookeeper的作用
  3. 详解javascript 存储
  4. IOS自学
  5. python中isort的使用
  6. Activity生命周期(二)
  7. Android Camera
  8. android style=&quot;@[package:]style/style_name&quot; ----------styles.xml
  9. Android读写JSON格式的数据之JsonWriter和JsonReader
  10. Linux系统调用过程
  11. java中进程与线程的区别
  12. linux磁盘管理增加,扩容
  13. CDOJ 1962 天才钱vs学霸周2【最大流】
  14. hive sql 里面的注释方式
  15. jquery异步ajax超大长度base64图片长字段数据传输问题解决办法和php后台处理办法
  16. Postgres使用ALTER USER命令修改用户的密码、密码过期,锁定,解锁
  17. 网络名词拾遗--part2
  18. 机器学习 python库 介绍
  19. 【NOI 2018】屠龙勇士(扩欧)
  20. JVM 体系结构介绍

热门文章

  1. [51单片机] nRF24L01 无线模块 测试 按键-灯-远程控制
  2. Backbone事件管理——Backbone.Events模块API结构
  3. atitit. orm mapping cfg 映射配置(3)-------hbnt one2maney cfg
  4. node.js cluster多进程、负载均衡和平滑重启
  5. Java band [Cite]
  6. bzoj 4300: 绝世好题
  7. [原创]软件质量保证圈QQ群:197915314
  8. 使用MFC提供的Http类下载和上传文件
  9. Serializable 和 Parcelable 区别
  10. Gabor学习笔记