0

I want to set a alarm in the mean while when app goes background,Alarm should keep on running,When the alarm time expires,I want trigger a http request.Is that possible? If so should I use ASIHttprequest? If I use ASIHttprequest will my app will be rejected?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Dinesh Kumar
  • 73
  • 1
  • 3
  • 13
  • What language? What OS? Please add more info to your post and tags. – Jeff Sep 28 '10 at 18:05
  • Your app will not be rejected for using the current version of ASIHttpRequest. There was a problem last year with it using a private API call (see http://groups.google.com/group/asihttprequest/browse_thread/thread/41ca2c6892fe183a), but it has been corrected. Note however that if you download too much data per minute, your app may be rejected. See http://stackoverflow.com/questions/1236788/iphone-app-rejected-for-transferring-excessive-volumes-of-data for more information. – Greg Sep 28 '10 at 19:41
  • Check answer at http://stackoverflow.com/questions/3291840/iphone-app-running-http-requests-while-application-in-background from a few minutes ago – John Smith Sep 28 '10 at 18:13

1 Answers1

0

I'm not sure why you need to do the stealth download while your app is backgrounded. What's wrong with doing it while your app is running? That said...

Yes, it's possible. There are various tricks you can use to keep your app running while i n the background; the easiest are "voip" provided you have an open socket (or just a UDP socket, or you might just tell it you have a keepalive) and "audio" (but you have to keep audio playing, even if all you play is silence).

I do not recommend using "voip" unless you have tested battery drain (it might be acceptable if you only connect/bind to localhost). AFAIK, Skype uses "voip"; it significantly reduces battery life (around 16-24 hours on iPhone 4; I haven't waited long enough to find out).

I do not recommend using "audio" — I think you need to play something (e.g. silence) for it to work, which probably stops the iPod app. It almost certainly drains the battery.

If you want to delay the download for more than a few minutes, I don't think simple "background tasks" will work, since iOS gives you a maximum time (unless I'm wrong and it's based on CPU time used by your app, but I doubt it). You can check with something like this:

-(void)applicationDidEnterBackground:(UIApplication*)application
{
  UIBackgroundTaskIdentifier taskId = [application beginBackgroundTaskWithExpirationHandler:^{NSLog(@"Task expired");}];
  NSTimeInterval backgroundTime = application.backgroundTimeRemaining;
  NSLog(@"Background task for %g seconds ...", (double)backgroundTime);
  [NSThread sleepForTimeInterval:backgroundTime - 1];
  NSLog(@"Background task finishing with %g seconds remaining ...", (double)application.backgroundTimeRemaining);
  [application endBackgroundTask:UIBackgroundTaskIdentifier];
}

Please, think of the children battery!

tc.
  • 33,468
  • 5
  • 78
  • 96