检测应用程序何时从锁屏与 iOS7 上的其他应用程序变为活动状态 [英] Detecting when app is becoming active from lockscreen vs other on iOS7

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

问题描述

我的应用在从锁屏激活(激活时锁定)或从其他任何位置激活时具有不同的行为.

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

在 iOS 6 及更低版本我可以检测到这一点

On iOS 6 and lower I could detect this

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 解除之前在开发论坛上发布了此内容,请参阅此处

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

推荐答案

我能够对此进行破解,到目前为止似乎是可靠的.它仅适用于设备,不适用于模拟器,并且已经在运行 iOS 7 的 iPhone 5s、5 和 4S 上进行了测试.

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 上似乎无法检测到应用程序的启动位置,但 有一种方法可以检测您是否要去锁屏与跳板.诀窍是在 applicationDidEnterBackground 中读取屏幕亮度.当应用程序由于按下锁定按钮或自动锁定超时而进入后台时,iOS 7 上的亮度将为 0.0.否则,当按下主页按钮或从多任务选择器启动另一个应用程序时亮度将 > 0或通知中心.

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

现在我有一个保存此信息的 ivar,我可以在 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 以检测您来自哪里,这个解决方案回答了类似但不完全相同的问题,即应用程序在后台运行时您要去哪里.例如,应用程序可能由于屏幕锁定超时而在后台运行,但随后另一个应用程序的通知唤醒了设备,用户直接从锁定屏幕转到那里,然后返回我的应用程序.我的应用程序会在后台确定用户进入锁屏,但是当他们回来时,他们实际上是来自活动屏幕.对于我的应用,这种差异可以忽略不计,但您的里程可能会有所不同.

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

我不确定亮度读数有多可靠,或者它是否会在未来的操作系统版本中发生变化,但与此同时,这个 hack 似乎是我们能得到的最好的.希望这会有所帮助.

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.

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

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