1

I setup a background task with beginBackgroundTaskWithExpirationHandler:^{ } and even after ending the task using

    if ([[UIDevice currentDevice] isMultitaskingSupported]) {
    [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
    backgroundTaskID = UIBackgroundTaskInvalid;
    }

    NSLog(@"App State -- %d", [[UIApplication sharedApplication] applicationState]);


    if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
    {
       //OpenGL operations
    }

what I get is UIApplicationStateActive. Is this a bug ? How else do I determine the app is indeed in background ?

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131

2 Answers2

1

Instead of relying on the applicationState value, how about posting a custom notification at the end of the background task?

Set up as an observer for your custom notification:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgroundTaskDidFinish:) name:@"BackgroundTaskDidFinish" object:<either nil or the object that will post the notification>];
}

Post your custom notification when the background task completes

if ([[UIDevice currentDevice] isMultitaskingSupported]) {
    [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
    backgroundTaskID = UIBackgroundTaskInvalid;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BackgroundTaskDidFinish" object:self]
}

Respond to the notification, which your app should receive when it really returns to an active state

- (void)backgroundTaskDidFinish:(NSNotification *)notification
{
   //OpenGL operations
}

Alternatively, you could look for one of the other notification messages that would indicate that your app is truly in an active state.

For instance, add your object as an observer for UIApplicationWillEnterForeground. Set a BOOL value when you receive that notification. Then, look for that BOOL value along with the UIApplicationState value.

Of course, at some point you'll have to clear the BOOL - say, when you're starting that background task - so it will be NO unless the app really did become active.

leanne
  • 7,940
  • 48
  • 77
0

You can try to determine it with NSNotification:

- (void)viewDidLoad {
[super viewDidLoad];
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
}
- (void)applicationWillResignActive:(NSNotification *)notification {
//do what you want when your app is in background
}
  • No it doesn't work, I already have that. The problem is that applicationWillResignActive: is not called before background task quits. – Deepak Sharma Nov 26 '13 at 08:25
  • To better illustrate the problem, I need to check if the app is really in state UIApplicationStateActive and do some OpenGLES rendering stuff. The problem is that the application status remains UIApplicationStateActive even while app is executing in background if I use beginBackgroundTaskWithApplicationHandler:^{}, even after performing endBackgroundTask. You can see in the code snippet in the message. – Deepak Sharma Nov 26 '13 at 08:51