0

I need to be able to detect when an App has fully transitioned from background to foreground. I tried using - (void)applicationDidBecomeActive:(UIApplication *)application delegate, but the delegate seems to be called when the application first launches. Then, I tried using - (void)applicationDidBecomeActive:(UIApplication *)application, but it is called before the app has fully transitioned. In the end, I combined the two delegates and it seems to work well for me.

- (void)applicationWillEnterForeground:(UIApplication *)application {
    openFromBackground = YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    if (openFromBackground) {
        openFromBackground = NO;
        // do stuff
    }
}

However, I am not sure this is the best way to handle the situation. Any tips or suggestions are appreciated.

Curt Rand
  • 1,025
  • 1
  • 9
  • 27
  • see this for e.g : https://stackoverflow.com/questions/38971994/detect-if-the-application-in-background-or-foreground-in-swift/38972081#38972081 – Anbu.Karthik Sep 21 '18 at 04:45
  • https://stackoverflow.com/questions/3639859/handling-applicationdidbecomeactive-how-can-a-view-controller-respond-to-the – Anbu.Karthik Sep 21 '18 at 04:46
  • Why not set a variable in `applicationDidEnterBackground` and check it in `applicationWillEnterForeground`? – rmaddy Sep 21 '18 at 05:38
  • 1
    What you're doing to differentiate from launching the app is fine (there's no notification or callback that suits the case you're looking for, so that's your only choice anyway). I'd be more curious about what action you want to perform after becoming active from the background, why it needs to be done after the app is active, and why you think the AppDelegate is the appropriate place to do it? The reason willEnterForeground is the only callback you get, is because your app is supposed to be ready to use *by the time It becomes active*, not some time after. – Pete Morris Sep 21 '18 at 08:23

0 Answers0