0

My app execute background fetches to update some data. In my initial view controller's viewDidLoad method I want to detect wether my app runs for background fetch or not?

I know that it's very easy to check the app's state using this:

if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
    return; // have a breakpoint here <----
}

But for some reason even when I launch app normally sometimes program can reach this breakpoint. Which means that applicationState returns UIApplicationStateBackground. Maybe app launch happens at the moment when background fetch is running or something like that?

I don't want to execute code that in my viewDidLoad method when app runs in background mode, so I need this if block in viewDidLoad.

tagirkaZ
  • 469
  • 7
  • 19
  • 2
    [This](http://stackoverflow.com/questions/7937400/need-clarification-about-uiapplicationstate) will help you to understand all about UIApplicationState. – Mohd Prophet Feb 12 '15 at 10:18
  • According to this explanation applicationState couldn't be UIApplicationStateBackground when you launch app normally. So it didn't help to me. – tagirkaZ Feb 12 '15 at 13:44

1 Answers1

1

By the time your viewDidLoad method is called, it will be in the foreground (with a state either UIApplicationStateInactive or UIApplicationStateActive), so it doesn't make sense to ask for UIApplicationStateBackground in the viewDidLoad, for more info read this link.

enter image description here

Wilson
  • 9,006
  • 3
  • 42
  • 46
  • This isn't true. In Xcode do "Launch due to a background fetch event", and you'll find that UIApplicationState is .Background in viewDidLoad. – Erich Oct 04 '15 at 23:39