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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电脑买贵了应该怎么办 微信红包收不了钱怎么办 mac版excel卡死怎么办 锐捷网卡为空怎么办 淘宝上买东西卖家不发货怎么办 快递员没给单号怎么办 淘宝物流把货弄丢了怎么办 刚买的卡没激活怎么办 腾讯王卡40g用完怎么办 手机欠费不知道电话号码怎么办 手机欠费了不用了怎么办 闲鱼恶意差评怎么办 淘宝买家账户体检中心违规怎么办 淘宝卖家账户体检中心违规怎么办 淘宝好评被删除评价怎么办 淘宝没收到货确认收货了怎么办 美团评论被删怎么办 拼多多恶意差评怎么办 淘宝评论被删了怎么办 淘宝买东西错怪店家了怎么办 淘宝骗删除差评怎么办 淘宝买家号虚假交易违规怎么办 淘宝商家一直不发货怎么办 淘宝商家不发货也不退款怎么办 淘宝买家恶意差评怎么办 淘宝评价完了忘截图了怎么办 12306app登录不上怎么办 淘宝换绑支付宝失败怎么办 淘宝和手机不兼容怎么办 换号之后微信怎么办 手机不兼容的应用程序怎么办 微信版本低登录不了怎么办 软件与系统不兼容怎么办 软件与手机系统不兼容怎么办 qq和手机不兼容怎么办 来个软件不兼容怎么办 安卓8.0不兼容app怎么办 两条内存不兼容怎么办 王者荣耀软件不兼容怎么办 冒险岛不兼容win7怎么办 百度网盘手机号换了怎么办