当检测应用程序从锁屏变得活跃VS其他的iOS7 [英] Detecting when app is becoming active from lockscreen vs other on iOS7

查看:156
本文介绍了当检测应用程序从锁屏变得活跃VS其他的iOS7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用成为从锁屏时激活具有不同的行为(锁定,同时有效),或成为积极从其他任何东西。

My app has different behavior when becoming active from the lockscreen (locked while active), or becoming active from anything else.

在iOS 6

UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (UIApplicationStateInactive == state)
    // Coming from locked screen (iOS 6)
else
    // Coming from Springboard, another app, etc...

但在iOS 7,状态值为 UIApplicationStateBackground 在这两种方案。这是预期的行为?我怎样才能正确地检测是否该应用程序是从锁屏现在推出?

But on iOS 7, the state value is UIApplicationStateBackground in both scenarios. Is this the intended behavior? How can I properly detect whether the app is launching from the lockscreen now?

注册开发者,我已经张贴此之前的NDA解除了devforums,请参阅这里

Registered devs, I already posted this on the devforums before the NDA was lifted, see here

推荐答案

我能找出一个黑客在这个到目前为止似乎是可靠的。它只适用的设备,而不是模拟器上,并已在iPhone 5S,5和4S运行iOS 7。测试

I was able to figure out a hack on this and so far seems to be reliable. It only works on the device, not the simulator, and has been tested on an iPhone 5s, 5 and 4S running iOS 7.

这似乎是没有可能的方式来检测应用程序是从在iOS 7推出,但有的一种方式,如果你是检测的的锁屏VS跳板。关键是要读 applicationDidEnterBackground 屏幕亮度。当应用程序打背景由于pssed锁定按钮时$ P $或自动锁定超时,亮度将在iOS 7 0.0,否则,这将是> 0的时候,家里按钮pssed $ P $或其他应用从多任务切换或通知中心推出。

It seems that there is no possible way to detect where the app is being launched from on iOS 7, but there is a way to detect if you are going to the lock-screen vs springboard. The trick is to read the screen brightness in applicationDidEnterBackground. When the app hits the background due to the lock button being pressed or an auto-lock timeout, the brightness will be 0.0 on iOS 7. Otherwise, it will be > 0 when the home button is pressed or another app launched from the multitask selector or notification center.

- (void)applicationDidEnterBackground:(UIApplication *)application {
    CGFloat screenBrightness = [[UIScreen mainScreen] brightness];
    NSLog(@"Screen brightness: %f", screenBrightness);
    self.backgroundedToLockScreen = screenBrightness <= 0.0;
}

现在,我有伊娃拿着这个信息,我可以在 applicationWillEnterForeground 用它来确定我的应用程序流量。

Now that I have an ivar holding this info, I can use it in applicationWillEnterForeground to determine my app flow.

- (void)applicationWillEnterForeground:(UIApplication *)application {
    if (self.backgroundedToLockScreen) {
        ... // app was backgrounded to lock screen
    } else {
        ... // app was backgrounded on purpose by tapping the home button or switching apps.
    }
    self.backgroundedToLockScreen = NO;
}

这是不完全相同的,虽然在iOS 6中的行为。在iOS 6中,您可以检查 UIApplicationState 来检测你的到来,这种解决方案回答了类似,但不正是你要的时候一样,问题该应用程序是后台运行。例如,也许应用在后台运行,由于屏幕锁定超时,但随后对其他应用的通知醒来的设备,用户去那里直接从锁屏,然后回到我的应用程序。我的应用程序将在一个后台用户去了锁屏确定,但他们回来的时候,他们实际上是由活动的屏幕来。对于我的应用程序,这种差异可以忽略不计,但你milage可能会有所不同。

It is not the exact same as the iOS 6 behavior though. On iOS 6, you could inspect the UIApplicationState to detect where you were coming from, and this solution answers the similar, but not exactly the same, question of where you were going when the app was backgrounded. For example, perhaps the app was backgrounded due to a screen lock timeout, but then a notification for another app woke the device, and the user went there directly from the lockscreen, then back to my app. My app would have determined on backgrounding that the user went to the lockscreen, but when they come back they are actually coming from an active screen. For my app, this difference is negligible, but your milage may vary.

所以有关旧操作系统的支持呢?我的应用程序还支持iOS 6的,所以我需要得到旧的行为了。简单。只需将应用程序状态监控前台方式:

So what about older OS support? My app also supports iOS 6 so I needed to get the old behavior too. Simple. Just the application state monitoring to the foreground method:

- (void)applicationWillEnterForeground:(UIApplication *)application {
    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
    if (UIApplicationStateInactive == state ||  // detect if coming from locked screen (iOS 6)
        self.backgroundedToLockScreen)          // detect if backgrounded to the locked screen (iOS 7)
    {
        ... // app is coming from or was backgrounded to lock screen
    } else {
        ... // app was backgrounded on purpose by tapping the home button or switching apps
    }
    self.backgroundedToLockScreen = NO;
}

我不知道的亮度读数的可靠性如何,它或者是否会在将来改变操作系统版本,但在此期间,该黑客似乎是我们可以得到最好的。希望这有助于。

I am not sure how reliable the brightness reading is, or whether or not it will change in future OS builds, but in the meantime, this hack seems to be the best we can get. Hope this helps.

这篇关于当检测应用程序从锁屏变得活跃VS其他的iOS7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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