简体   繁体   中英

background http requests on ios for “killed app”

I read a lot about iOS backgrounding but I still have issues understanding the concept for one specific case .

When my app is killed with taskmanager (or by iOS because of memory pressure) and a significantLocationChange or GeoFence happens, my whole app is started by iOS and my callback method is called.

First question here is: what state am I in? Even the UI is partly loaded so I'm kind of foreground, but since there is nothing on the display, I consider it being background.

My second and main question is, how can I send http requests AND resend them if the internet connection is down currently?

I have tried to use [NSThread sleepWithInterval] , dispatch with delay and NSTimer . All of them with the same bad result: It works great in foreground, great in NORMAL background (user pressed homebutton) but very bad in the state described above. I'm able to send multiple http requests if they run fast enough but when I need several seconds with sleep, I get killed randomly. When I use a Timer with 2 second delay, I'm able to reach my "resend http request" method 2-3 times bevor all output of the app stops. If I use my planned delay of 30 seconds, I don't even get the timer call once.

So a last question is: Is there a backgroundpermission that I need to register? I already use

<key>UIBackgroundModes</key>
<array>
   <string>location</string>
</array>

Or would it help to use NSOperationQueue? Does it have special permissions that I don't have?

You can check the application state by calling

[[UIApplication sharedApplication] applicationState]

In your case it should be UIApplicationStateBackground .


If you using a NSURLSession for your network requests, you can set it up with a background session configuration. When your app is in the background, this will allow networks requests to be handled by the system even when your app isn't running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM