12

当我的应用程序从锁屏激活(激活时锁定)或从其他任何东西激活时,我的应用程序有不同的行为。

在 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在这两种情况下都存在。这是预期的行为吗?如何正确检测应用程序现在是否从锁屏启动?

注册开发者,在 NDA 解除之前,我已经在开发者论坛上发布了此内容,请参见此处

4

2 回答 2

10

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

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

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

现在我有一个保存此信息的 ivar,我可以使用它applicationWillEnterForeground来确定我的应用程序流程。

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

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

- (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 似乎是我们能得到的最好的。希望这可以帮助。

于 2013-10-28T14:46:39.890 回答
-1

Actually the only proper way to set your app behavior when becoming active is via the app delegate methods.

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

These two are called when the app is running in the background and becomes active either via multitasking UI or after a call or other interruption.

When the app is opened from the Springboard and is not running in the background this method is called:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}
于 2013-10-26T18:33:39.690 回答