首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从锁屏与iOS7上的其他应用检测应用何时变为活动状态

从锁屏与iOS7上的其他应用检测应用何时变为活动状态
EN

Stack Overflow用户
提问于 2013-10-18 22:53:15
回答 2查看 12.1K关注 0票数 12

我的应用程序在从锁屏变为活动状态(在活动状态下被锁定)或从其他位置变为活动状态时具有不同的行为。

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

代码语言:javascript
复制
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (UIApplicationStateInactive == state)
    // Coming from locked screen (iOS 6)
else
    // Coming from Springboard, another app, etc...

但在iOS 7上,两种情况下的状态值都是UIApplicationStateBackground。这是预期的行为吗?我如何才能正确地检测应用程序现在是否从锁屏启动?

注册的开发人员,在保密协议解除之前,我已经在开发论坛上发布了这篇文章,请参阅here

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-28 22:46:40

我想出了一个关于这个的破解方法,到目前为止似乎是可靠的。它只能在设备上运行,而不能在模拟器上运行,并且已经在运行iOS 7的iPhone 5s、5和4S上进行了测试。

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

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

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

代码语言:javascript
复制
- (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,所以我也需要获得旧的行为。很简单。仅监控应用程序状态到前台方法:

代码语言:javascript
复制
- (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;
}

我不确定亮度读数有多可靠,也不确定它是否会在未来的操作系统版本中改变,但在此期间,这次黑客攻击似乎是我们能得到的最好的。希望这能有所帮助。

票数 10
EN

Stack Overflow用户

发布于 2013-10-27 02:33:40

实际上,在激活时设置应用程序行为的唯一正确方法是通过应用程序委托方法。

代码语言:javascript
复制
- (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.
}

这两个函数在应用程序在后台运行时被调用,并通过多任务UI或在调用或其他中断后变为活动状态。

当应用程序从Springboard打开且未在后台运行时,此方法将被调用:

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19452696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档