【发布时间】:2012-11-16 23:05:29
【问题描述】:
有没有办法判断 iOS 应用是通过快速应用切换还是手动进入前台?我需要知道调用 applicationWillEnterForeground 的时间,因此可以根据应用进入前台的条件执行(或不执行)某些特定代码。
编辑: 事实证明,这对我来说更像是一个设计问题。我将代码移至 applicationDidBecomeActive。我还向名为 fastAppSwitching 的 appDelegate 添加了一个 BOOL 属性(可能是错误的名称)。我在 application:handleOpenURL 和 application:openURL:sourceApplication:annotation 中将此设置为 YES。然后我在应用程序中添加了以下代码:didFinishLaunchingWithOptions:
if (launchOptions) {
self.fastAppSwitching = YES;
}
else {
self.fastAppSwitching = NO;
}
在applicationDidBecomeActive中,我使用了以下代码:
if (fastAppSwitching == YES) {
self.fastAppSwitching = NO; //stop, don't go any further
}
else {
...
}
EDIT2:MaxGabriel 在下面提出了一个很好的观点:“只是对采用此处描述的解决方案的其他人的警告,applicationDidBecomeActive:在用户例如忽略电话或短信时被调用,这与 applicationWillEnterForeground 不同”。这实际上也适用于应用内购买和 Facebook 应用内授权(iOS 6 中的新功能)。因此,经过一些进一步的测试,这是当前的解决方案:
添加一个名为passedThroughWillEnterForeground 的新Bool。
在applicationWillResignActive中:
self.passedThroughWillEnterForeground = NO;
在 applicationDidEnterBackground 中:
self.passedThroughWillEnterForeground = NO;
在应用程序WillEnterForeground:
self.passedThroughWillEnterForeground = YES;
在 applicationDidBecomeActive 中:
if (passedThroughWillEnterForeground) {
//we are NOT returning from 6.0 (in-app) authorization dialog or in-app purchase dialog, etc
//do nothing with this BOOL - just reset it
self.passedThroughWillEnterForeground = NO;
}
else {
//we ARE returning from 6.0 (in-app) authorization dialog or in-app purchase dialog - IE
//This is the same as fast-app switching in our book, so let's keep it simple and use this to set that
self.fastAppSwitching = YES;
}
if (fastAppSwitching == YES) {
self.fastAppSwitching = NO;
}
else {
...
}
EDIT3:我认为我们还需要一个布尔值来判断应用程序是否从终止状态启动。
【问题讨论】:
-
“手动”是什么意思?快速应用切换不也是手动完成的吗?
-
我的术语可能不正确,但我知道 Facebook、Dropbox 和其他第 3 方通常会在应用内实现快速应用切换以进行登录。
-
哦,我明白了。所以你的意思是你想知道你的应用是由另一个应用还是用户启动的,对吧?
-
我打算向您指出该方法...好吧,在这种情况下是 IDK。但是为什么你需要
applicationWillEnterForeground;呢?我在这里闻到了设计问题。 -
只是对采用此处描述的解决方案的其他人的警告,
applicationDidBecomeActive:在用户例如调用时被调用忽略电话或短信,不像applicationWillEnterForeground
标签: iphone ios facebook background fast-app-switching