区分iOS7上的屏幕锁定和主页按钮按下

编程入门 行业动态 更新时间:2024-10-23 23:31:51
本文介绍了区分iOS7上的屏幕锁定和主页按钮按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在 applicationDidEnterBackground 中做一些事情.但我需要区分哪些用户操作会导致进入背景":屏幕锁定或主页按钮按下.

I need to do something in applicationDidEnterBackground. But I need to differentiate which user action causes the "enter background": screen lock or home button press.

我正在使用此代码,该代码来自此帖子 - 如何区分 iOS5 上的屏幕锁定和主页按钮按下?:

I was using this code, which is from this post - How to differentiate between screen lock and home button press on iOS5?:

UIApplicationState state = [application applicationState]; if (state == UIApplicationStateInactive) { NSLog(@"Sent to background by locking screen"); } else if (state == UIApplicationStateBackground) { NSLog(@"Sent to background by home button/switching to other app"); }

它在 iOS6 上运行良好.但是在 iOS7(设备和模拟器)上,我总是得到 UIApplicationStateBackground,无论用户点击主页还是锁定按钮.

It works fine on iOS6. but on iOS7 (both device and simulator), I always get UIApplicationStateBackground, whether the user clicks the home or the lock button.

有人知道是什么导致了这种情况吗?iOS 7 更新多任务后台处理?或者我的应用程序的某些设置(我的应用程序的后台模式已关闭)?

Does someone have an idea about what could cause this? iOS 7 updates to multi-task background handling? Or some setting of my app (my app's background mode is off)?

还有其他解决方案吗?

推荐答案

这可以帮助你在 iOS6 和iOS7 :).

This can help you both on iOS6 & iOS7 :).

当用户按下锁定按钮时,您将收到 com.apple.springboard.lockcomplete 通知.

When user press lock button you will get a com.apple.springboard.lockcomplete notification.

//new way //put this in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayStatusChanged, CFSTR("com.apple.springboard.lockcomplete"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); //put this function in AppDelegate static 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 onAppEnterBackground UIApplicationState 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];

CGFloat screenBrightness = [[UIScreen mainScreen] brightness]; NSLog(@"Screen brightness: %f", screenBrightness); UIApplicationState state = [[UIApplication sharedApplication] applicationState]; if (state == UIApplicationStateInactive) { NSLog(@"Sent to background by locking screen"); } else if (state == UIApplicationStateBackground) { if (screenBrightness > 0.0) { NSLog(@"Sent to background by home button/switching to other app"); } else { NSLog(@"Sent to background by locking screen"); } }

更多推荐

区分iOS7上的屏幕锁定和主页按钮按下

本文发布于:2023-11-27 03:07:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636314.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按下   按钮   屏幕   主页

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!