在iOS7上区分屏幕锁定和主屏幕按钮 [英] Differentiate between screen lock and home button press on iOS7

查看:182
本文介绍了在iOS7上区分屏幕锁定和主屏幕按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 applicationDidEnterBackground 中执行一些操作。但我需要区分哪个用户操作导致输入背景:屏幕锁定或主页按钮。



我使用这个代码,这是从帖子 - 如何区分iOS5上的屏幕锁定和主页按钮按钮?

  UIApplicationState state = [application applicationState]; 
if(state == UIApplicationStateInactive){
NSLog(@通过锁定屏幕发送到后台);
} else if(state == UIApplicationStateBackground){
NSLog(@通过主页按钮发送到后台/切换到其他应用程序);
}

在iOS6上可以正常工作。但在iOS7(设备和模拟器)上,我总是得到 UIApplicationStateBackground ,无论用户是点击home还是lock按钮。



有人知道什么可能导致这种情况吗? iOS 7更新多任务后台处理?



还有其他解决方案吗?

解决方案

这可以帮助你在iOS6& iOS7:)。



当用户按下锁定按钮时,您会得到一个 com.apple.springboard.lockcomplete 通知。

  // new way 
//将这放入 - (BOOL)应用程序:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
displayStatusChanged,
CFSTR(com.apple.springboard.lockcomplete),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);

//将此函数放在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];
}
}

//放在onAppEnterBackground
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if(state == UIApplicationStateInactive){
NSLog(@通过锁定屏幕发送到后台);
} else if(state == UIApplicationStateBackground){
if(![[NSUserDefaults standardUserDefaults] boolForKey:@kDisplayStatusLocked]){
NSLog(@切换到其他应用程序);
} else {
NSLog(@通过锁定屏幕发送到后台);
}
}

//将这个放在 - (void)applicationWillEnterForeground:(UIApplication *)application
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@ kDisplayStatusLocked];
[[NSUserDefaults standardUserDefaults] synchronize];

  CGFloat screenBrightness = [[UIScreen mainScreen] brightness]; 

NSLog(@屏幕亮度:%f,screenBrightness);

UIApplicationState state = [[UIApplication sharedApplication] applicationState];

if(state == UIApplicationStateInactive){

NSLog(@通过锁定屏幕发送到后台);

} else if(state == UIApplicationStateBackground){
if(screenBrightness> 0.0){
NSLog(@通过主页按钮发送到后台/切换到其他应用程序 );
} else {
NSLog(@通过锁定屏幕发送到后台);
}
}

b

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.

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");
}

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.

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)?

And is there an alternative solution?

解决方案

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

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上区分屏幕锁定和主屏幕按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆