0

I want to monitor the headphone jack in my App, I have the code for do it, but this only works when the App is active, and I need to do it even if the App is inactive. Is that possible?

In my test I put the code for monitoring in the AppDelegate and when I unplug the jack it fires the "NSLog" that I put for that case, and if I plug it another NSLog is launched, but when the user press the "power button" I understand than my App is now "inactive" and the code for the monitoring is not working at the time.

Is there a posibility to create a background task for this purpose that work even if the App is inactive?

  • You can use background tasks for that, but any background task will be terminated after a maximum of 10 minutes, is that okay for your app? – Fabian Kreiser Feb 05 '13 at 16:25
  • Check the state of the headphone jack when your app returns to the foreground. – rmaddy Feb 05 '13 at 18:26
  • Thanks for the answers; @FabianKreiser maybe that could works, but I don't know very well about "Background tasks", I need launch a notification every time that the headphone jack is unplugged, is possible with background tasks? – Roberto Briones Argüelles Feb 05 '13 at 19:19
  • @rmaddy: For my purpose that doesn'tn work, because I want to do the monitoring even if my App is inactive for a few minutes at least. – Roberto Briones Argüelles Feb 05 '13 at 19:21
  • You can fire notifications using UILocalNotification while your app is in the background. You'll be able to monitor the headphone jack for ten minutes and fire notifications during that time, I'll post a more detailed answer tomorrow. – Fabian Kreiser Feb 05 '13 at 19:28

1 Answers1

0

You can run your app for up to ten minutes after the app went into the background using a background task. Take a look at the Background Execution and Multitasking section of the iOS App Programming Guide.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
        // clean up any unfinished task business, your app is going to be killed if you don't end the background task now

        [application endBackgroundTask:backgroundTask];
        backgroundTask = UIBackgroundTaskInvalid;
    }];

    // start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Monitor the head phone jack here.
        // If this happens asynchronously, you don't need to dispatch this block, your app will continue to run as normal, only modifying or accessing its UI while it's in background mode is prohibited.

        // End the background task if you're done. If this is never the case, your expiration handler will be called after ten minutes.
        [application endBackgroundTask:backgroundTask];
        backgroundTask = UIBackgroundTaskInvalid;
    });
}

You can notify the user by sending a UILocalNotification:

- (void)notifyUserWhilePerformingBackgroundTask
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"Headphone removed!";

    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60
  • Thank you very much, I will try that, I only don't understand the part of "clean up any unfinished task business, your app is going to be killed if you don't end the background task now"... Which "background task"? if at the time the App doesn't have any background task – Roberto Briones Argüelles Feb 06 '13 at 17:02
  • That's inside the expiration handler block. That block will be called after ten minutes have passed and you didn't end the background task yet. – Fabian Kreiser Feb 06 '13 at 17:05