简体   繁体   English

如何判断iOS应用程序是否从快速应用程序切换或手动进入前台?

[英]How to tell if leaving iOS app entered foreground from fast-app switching or manually?

Is there a way to tell if an iOS app enters the foreground from fast-app switching or manually? 有没有办法判断iOS应用程序是通过快速应用程序切换还是手动进入前台? I need to know by the time applicationWillEnterForeground is called, so some specific code can be executed (or not executed) depending on the condition in which the app entered the foreground. 我需要在调用applicationWillEnterForeground时知道,因此某些特定代码可以执行(或不执行),具体取决于应用程序进入前台的条件。

EDIT: It turned out that this was more of a design issue for me. 编辑:事实证明这对我来说更像是一个设计问题。 I moved my code to applicationDidBecomeActive. 我将代码移到了applicationDidBecomeActive。 I also added a BOOL property to the appDelegate called fastAppSwitching (probably the wrong name for it). 我还在appDelegate中添加了一个名为fastAppSwitching的BOOL属性(可能是错误的名称)。 I set this to YES in application:handleOpenURL and application:openURL:sourceApplication:annotation. 我在应用程序中将此设置为YES:handleOpenURL和application:openURL:sourceApplication:annotation。 Then I added the following code to application:didFinishLaunchingWithOptions: 然后我将以下代码添加到应用程序:didFinishLaunchingWithOptions:

        if (launchOptions) {
            self.fastAppSwitching = YES;
        }
        else {
            self.fastAppSwitching = NO;
        }

In applicationDidBecomeActive, I used the following code: 在applicationDidBecomeActive中,我使用了以下代码:

    if (fastAppSwitching == YES) {
        self.fastAppSwitching = NO; //stop, don't go any further
    }
    else {
       ...
    }

EDIT2: MaxGabriel makes a good point below: "Just a warning to others taking the solution described here, applicationDidBecomeActive: is called when the user eg ignores a phone call or text message, unlike applicationWillEnterForeground". 编辑2:MaxGabriel在下面提出了一个很好的观点:“只是警告其他人采取此处描述的解决方案,当用户例如忽略电话或短信时,会调用applicationDidBecomeActive:,与applicationWillEnterForeground不同”。 This is actually also true for in-app purchases and Facebook in-app authorization (new in iOS 6). 这实际上也适用于应用内购买和Facebook应用内授权(iOS 6中的新功能)。 So, with some further testing, this is the current solution: 因此,通过一些进一步的测试,这是当前的解决方案:

Add a new Bool called passedThroughWillEnterForeground. 添加一个名为passThroughWillEnterForeground的新Bool。

In applicationWillResignActive: 在applicationWillResignActive中:

self.passedThroughWillEnterForeground = NO;

In applicationDidEnterBackground: 在applicationDidEnterBackground中:

self.passedThroughWillEnterForeground = NO;

In applicationWillEnterForeground: 在applicationWillEnterForeground中:

self.passedThroughWillEnterForeground = YES;

In applicationDidBecomeActive: 在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: I think we also need a bool to tell if app was launched from terminated. EDIT3:我认为我们还需要一个bool来判断app是否是从终止发起的。

If your application is launched by another application, the 如果您的应用程序是由另一个应用程序启动的

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
  sourceApplicarion:(NSString *)bundleID
         annotation:(id)info;

method is called on your app delegate. 在您的应用委托上调用方法。 You can use this method to eg set a Boolean switch to true that indicates whether the app was launched by another program. 您可以使用此方法例如将布尔值开关设置为true,以指示应用程序是否由其他程序启动。

The peoblem is that this method is called after applicationWillEnterForeground: , so you can't tell in that method whether your app was launched manually or automatically. 问题是这个方法是 applicationWillEnterForeground: 之后调用的,所以你不能告诉那个方法你的app是手动启动还是自动启动。

However, I suspect that if you need this to be detected in a particular method, you may have a design problem and you should probably reorganize your code. 但是,我怀疑如果您需要在特定方法中检测到这一点,则可能存在设计问题,您可能应该重新组织代码。

如果您的应用程序是从其他应用application:openURL:sourceApplication:annotation打开的,则application:openURL:sourceApplication:annotation将在您的应用程序委托上调用application:openURL:sourceApplication:annotation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM