iOS_获取&区分iOS锁屏与Home事件
来源:互联网 发布:五大联赛数据 编辑:程序博客网 时间:2024/05/19 16:27
今天在研究获取是否锁屏状态,没找到方法。有找到的说要越狱,没越狱的方法又只能获取到响应通知,不能识别是锁还是解。无意间看到这么个文章,测试了一下真机可以运行就收藏留着后面备用吧!(测试环境:iPhone5s)
原文来自:http://wangqingpeng.info/blog/2013/11/04/huo-qu-and-qu-fen-iossuo-ping-yu-homeshi-jian/
//put this in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsCFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayStatusChanged, CFSTR("com.apple.springboard.lockcomplete"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);//put this function in AppDelegatestatic void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { if (name == CFSTR("com.apple.springboard.lockcomplete")) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kDisplayStatusLocked"]; [[NSUserDefaults standardUserDefaults] synchronize]; }}//put this in onAppEnterBackgroundUIApplicationState state = [[UIApplication sharedApplication] applicationState]; if (state == UIApplicationStateInactive) { NSLog(@"Sent to background by locking screen"); } else if (state == UIApplicationStateBackground) { if (![[NSUserDefaults standardUserDefaults] boolForKey:@"kDisplayStatusLocked"]) { NSLog(@"Sent to background by home button/switching to other app"); } else { NSLog(@"Sent to background by locking screen"); } }//put this in - (void)applicationWillEnterForeground:(UIApplication *)application[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"kDisplayStatusLocked"];[[NSUserDefaults standardUserDefaults] synchronize];
上面那个只能响应锁屏、点击home键进入后台。
以下是自己综合网上的写出来的方法。
原文参考:http://blog.csdn.net/feng3327/article/details/8859478
通知方式判断解锁/锁屏:目前我只能在运行当前demo的时候响应通知,不能获取到状态,初步考虑是认为加入bool判断(因为,我觉得锁屏、解锁跟开关值是一个意思)。废话有点多,先上代码:
//这段写在:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, updateEnabled, CFSTR("com.apple.iokit.hid.displayStatus"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
//这段写在:AppDelegate 下static void updateEnabled(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) { NSLog(@"锁屏/解锁");}
0 0
- iOS_获取&区分iOS锁屏与Home事件
- iOS_获取&区分iOS锁屏与Home事件
- 获取&区分iOS锁屏与Home事件
- 如何区分锁屏和home
- iOS 捕捉home事件
- iOS之捕捉home事件
- iOS捕捉home键事件
- iOS 短信与电话事件的获取
- iOS 短信与电话事件的获取
- iOS 短信与电话事件的获取
- iOS 短信与电话事件的获取
- iOS touch事件单击双击区分响应
- iOS_ MonkeyTalk(1)(iOS与android的自动化测试工具)
- iOS 获取Home键指纹验证
- iOS_获取通讯录数据
- iOS_获取通讯录
- iOS_获取app版本号
- iOS_获取设备信息
- 【Object C】 基本数据类型
- 在CentOS 6.3中安装与配置Tomcat-7方法
- Android断点续传原理
- 文件路径分解与批处理文件遍历
- 怎样查看正在运行的进程所在的全路径
- iOS_获取&区分iOS锁屏与Home事件
- C++ 静态链表
- Android--详解Android动画之Tween Animation
- 自定义的照相机视图
- 关于armhf (hard-float ABI for ARM)
- Android NDK支持STL的一些注意事项
- float double 与0比较
- POJ 1511 Invitation Cards 图论题解
- ubuntu 下mkfs.jffs2安装