0

I am trying to implement certain app feature when app goes to background. But i saw a strange issue that if i do NSLog after every 2 seconds using NSTimer in "applicationDidEnterBackground", using simulator it works, but when i tested it in actual device, it doesn't print. Below is my code from "AppDelegate.m":

- (void)printLog
{
    NSLog(@"logging after 2 sec.");
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    bckTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(printLog) userInfo:nil repeats:YES];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [bckTimer invalidate];
}

Please let me know why it's happening like this and may be any tips you would like to share while working with background app feature execution.

Thanks

codejunkie
  • 53
  • 2
  • 10
  • You cannot just keep your app running like that, but Apple provides different background mechanisms for different sorts of background functionality (e.g. continuing lengthy downloads in the background; requesting just a few minutes to finish up some finite length task; background significant change location services; etc.). So, advice on how to proceed will be entirely dependent upon what you're trying to do in the background. – Rob Apr 05 '15 at 20:09
  • For more information, see [iOS App Programming Guide: Background Execution](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW1). – Rob Apr 05 '15 at 20:10
  • Thanks Rob. My actual motive is to fetch some data using AFNetworking API after >30sec while app is in background, and show updated contents in Today Extension. I just started it and saw that issue, so that's why I asked. – codejunkie Apr 05 '15 at 21:03
  • OK. I might be inclined to use background `NSURLSession` to get the data if you're downloading so much that it is going to take more than 30 sec. If you want to do this with AFNetworking, see http://stackoverflow.com/questions/21350125/afnetworking-2-0-and-background-transfers/21359684#21359684 for tips how to do background session with AFURLSessionManager. – Rob Apr 05 '15 at 21:35

0 Answers0