1

I'm trying to get an app running indefinitely.. I'm developing an internal app, so apple approval doesnt matter.

I've tried a few different things, including simulating a voip app using this tutorial..

http://www.raywenderlich.com/29948/backgrounding-for-ios

Eventually I ran into this code snippet online

[application beginBackgroundTaskWithExpirationHandler:^{}];

I place this in beginBackgroundTaskWithExpirationHandler and then call a slow-looping function inside of my app delegate in application:didFinishLaunchingWithOptions

-(void)caller{

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        NSTimeInterval timeRemaining = [UIApplication sharedApplication].backgroundTimeRemaining;
        NSLog(@"%f", timeRemaining);
       [self caller];
    });
}

this logs background time remaining every 3 seconds. it does this when i exit my app.. i see a countdown from 180 (3 minutes) to eventually 0

then it just keeps logging 0 indefinitely. I would figure if background time reaches zero, then my process wouldnt execute.

Anyone have any idea whats going on here?

hamobi
  • 7,940
  • 4
  • 35
  • 64
  • Your app *may* continue to operate in the background (as you are seeing) but if you load another app, particularly one that is memory intensive (*The Simpsons Tapped Out* is one of my favourites for testing :) ) then your app will be unloaded – Paulw11 Sep 01 '14 at 00:37
  • 1
    Did you launch this via Xcode? I believe the behavior will also be affected by running the app through the debugger. If you install the app on the device and then run it directly from the device, the behavior may change as well. – Rob Sep 01 '14 at 00:40
  • i ran this on my device but plugged into my computer. im looking at the log messages via xcode – hamobi Sep 01 '14 at 00:41
  • im running a 3d game on my phone.. i still see process being logged? haha.. – hamobi Sep 01 '14 at 00:42
  • 1
    Are you seeing these messages in Xcode's console, or via Xcode's Device Organizer? If you're seeing them in Xcode's standard debugging console, that means the app was started on the device via Xcode. On the Xcode's "Product" menu, is the "Stop" option grayed out? – Rob Sep 01 '14 at 00:44
  • im running the app on my device via xcode.. should i not be? how would i see log messages if I run the app directly off my phone? – hamobi Sep 01 '14 at 00:48
  • ahh nevermind.. i figured out how to do that.. let me see if there is a difference in behavior – hamobi Sep 01 '14 at 00:50

1 Answers1

0

If you use beginBackgroundTaskWithExpirationHandler and start the app from Xcode, that will alter the behavior of this UIBackgroundTaskIdentifier (Xcode will keep the app alive). If you see the log messages showing up in Xcode's debugging console, that means the app was started on the device via Xcode.

Now that the app is installed on the device, stop it (either by hitting the "stop" button in Xcode or by manually terminating the app via Springboard on the device). Once you've confirmed the app isn't running, you can now run it again by tapping the app icon on the device itself. It should start, not linked to Xcode in any way.

Even though the app is now not running via Xcode, though, you can still monitor the progress of what you logged, by going to Xcode's "Device Organizer", and open up the actual device's console. I suspect you'll see the background task expire, like you expected it to.

Rob
  • 415,655
  • 72
  • 787
  • 1,044