Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Connections while iOS app is in background and retrying if offline

I've written a basic location tracking app that will run in the background. (Not just a background thread, but when the device is dark.) I have the UIBackgroundModes key set and this part is working nicely writing locations to the screen.

Next, I need to process those locations. Based on location and some other criteria, the app will need to occasionally send some information to a server via HTTP get or post. There are a few challenges:

  1. The messages need to be sent to the server even if the app is in the background.

  2. The messages need to be sent to the server even if the device is offline when the message is generated (users will often be out of cell range while getting a gps location). The messages don't have to be sent immediately, of course, but they should be stored and sent when the device is back online.

  3. If the message does not make it the first try, it should be resent a few times before giving up.

I'm looking for help planning how to handle these three things.

The app will target iOS6+.

I'm a new iOS developer but I've developed a very similar app for android. I'm looking for general advice and strategies - not code unless there happens to be some out there already. I just don't know iOS / Objective C well enough yet to know if there are already classes or frameworks to handle this or help with aspects of it. I've seen plenty of "How to send data to a server" examples, that's not what I'm looking for. Instead, I'd like pointers to which classes or systems might help manage retrying connections/commands, how to handle this in the background, and so on. Also, warnings for issues to watch out for is helpful too. Many thanks for any advice.

like image 314
Scott Saunders Avatar asked Oct 04 '22 02:10

Scott Saunders


1 Answers

You're going to quickly find that you are a lot more restricted in what you can and can't do while the app is in the background. Your best resource will be the App States and Multitasking Guide in the iOS developer library. From the docs:

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that support Voice over Internet Protocol (VoIP)
  • Newsstand apps that need to download and process new content
  • Apps that receive regular updates from external accessories

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

Sounds like your app may qualify under the location bullet item. You can enable background location services by including the UIBackgroundModes key in your info.plist file, and you'll have to design the app to work well with background modes and background fetches (callbacks the system will provide, handled in your app delegate). Read up on "Tracking the User’s Location" section of the multitasking guide.

Also, there are some new considerations in iOS 7 to look into. If you have access to the member center, I recommend you check out the WWDC videos for more information.

like image 98
Kyle Clegg Avatar answered Oct 12 '22 10:10

Kyle Clegg