4

I'm trying to get the following functionality in my iPhone app:

  • When backgrounded, stays running (doesn't have to do any background work)
  • When resumed, app picks up where it was left off

I'm mainly wanting the same screen on my app still up, as there are several UINavigationControllers within a UITabBarController.

I have done all of the following:

  • Made sure I'm compiling with 4.1 SDK
  • Set UIApplicationExitsOnSuspend to false
  • Handle DidEnterBackground and WillEnterForeground in my AppDelegate
  • Call BeginBackgroundTask in DidEnterBackground, to attempt to keep my app open

I'm using MonoTouch, but that it probably beside the point. I can take answers in Obj-C, for sure.

I've tested my app on a jailbroken phone with Backgrounder, and I see the "app in background" badge disappear immediately after pushing the home button. I also tried setting UIBackgroundModes in my Info.plist, but to no avail.

Is there anything I'm missing?

Or is this something I would have to implement on my own to resume the previous state of my app? Everywhere I've read talks like it should just work automatically.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • While debugging on the device, WillTerminate is called almost immediately after pressing the home button. – jonathanpeppers Nov 15 '10 at 20:36
  • I am going to send this issue to the folks at MonoTouch, as a new simple repro app has the same issues. – jonathanpeppers Nov 15 '10 at 22:04
  • Ignoring Backgrounder, when you go from your application to another one, does your application go exactly back to where it was when you left? When you run the application through Xcode on the device, does debugging stop (the little red stop sign get greyed out) when you hit the home button on the device or Simulator? Are you running this on a device that supports multitasking (iPhone 3GS and newer)? – Brad Larson Nov 15 '10 at 22:45

4 Answers4

1

If you don't want to be doing work in background, don't call beginBackgroundTask. That call is for situations where you want to do some kind of work in the background. And if you don't finish that work fast enough, iOS will terminate your app.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • What is the minimum required to keep my same view on screen that the user left when backgrounding my app? – jonathanpeppers Nov 15 '10 at 21:01
  • If applicationWillResignActive and applicationDidResumeActive are both empty, that should happen automatically. – William Jockusch Nov 15 '10 at 21:07
  • @Jonathan - William is right, you don't need to make your application remain actively processing things in the background for it to return to the state you left it. This should be automatic just by building against the 4.x SDK. – Brad Larson Nov 15 '10 at 22:46
1

When I upgraded to iOS 4.x, my MT application started exhibiting this behavior without me having to do anything. iOS should take care of it for you.

Jason
  • 86,222
  • 15
  • 131
  • 146
1

I finally got in touch with someone on MonoTouch's irc.

In MonoDevelop there is an option to make a dual iPad/iPhone project, which I used. This is causing my app to behave as if it's running with the 3.2 SDK when deployed to the device.

I think my solution is to install the iOS 4.2 SDK that just came out, since this ads the new multi-tasking feature on iPad.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
0

Not only do you need to support going into the background, you also need to support cases where your app has been terminated. In your app’s initialization code, you should resume the state that it was in. For instance, when you push a view controller, use NSUserDefaults to store a value for the currently-displayed screen, and then when you start read that value and display the associated screen.

Jeff Kelley
  • 19,021
  • 6
  • 70
  • 80
  • I don't mind to restore the app to its former state after termination. I only want to do this when backgrounded. The reason is b/c my app can commonly switch to google maps, etc. – jonathanpeppers Nov 15 '10 at 22:03