HTTP connections while the iOS app is in the background and retries if offline

I wrote a basic location tracking application that will run in the background. (Not only the background stream, but when the device is dark.) I have a set of UIBackgroundModes keys, and this part works by beautifully recording places on the screen.

Then I need to process these locations. Based on location and some other criteria, the application will sometimes need to send some information to the server through an HTTP receive or publication. There are several problems:

  • Messages must be sent to the server, even if the application is in the background.

  • Messages must be sent to the server even if the device is disconnected when a message is generated (users will often be out of range of cells, receiving a gps location). Of course, messages should not be sent immediately, but they should be stored and sent when the device is reconnected to the network.

  • If the message does not make it the first attempt, it must be resent several times before giving up.

I am looking for help in planning how to handle these three things.

The app will target iOS6 +.

I am a new iOS developer, but I developed a very similar application for Android. I am looking for general tips and strategies, not code if it is no longer there. I just don't know iOS / Objective C well enough to find out if there are already classes or frameworks to handle this or help with its aspects. I saw many examples of "How to send data to the server", this is not what I am looking for. Instead, I would like to point out pointers for which classes or systems can help manage re-connections / commands, how to handle this in the background, etc. It is also helpful to warn of issues that need to be addressed. Thanks so much for any advice.

+6
source share
3 answers

You will quickly find that you are much more limited in what you can and cannot do while the application is in the background. Your best resource will be Applications and a multi-tasking guide in the iOS Developer Library. From the docs:

For tasks that require longer execution time, you must request specific permissions to run them in the background without pausing them. On iOS, only certain types of applications are allowed to run in the background:

  • Applications that play audio content for the user in the background, for example, an application for a music player.
  • Applications that inform users of their location at any time, such as a navigation application
  • Applications that support Voice over Internet Protocol (VoIP)
  • Newsstand apps to download and process new content
  • Applications regularly receiving updates from external accessories

Applications that implement these services must declare the services they support and use the system to implement the relevant aspects of these services. Service announcements let the system know what services you are using, but in some cases itโ€™s a system framework that actually prevents your application from pausing.

It looks like your application may qualify for a location marker element. You can enable background detection services by including the UIBackgroundModes key in your UIBackgroundModes , and you will need to develop an application to work well with background modes and background selections (the callbacks that the system will provide are handled in your application). Read the โ€œUser Location Trackingโ€ section of the multitasking guide.

In addition, there are several new considerations in iOS 7. If you have access to the member center, I recommend that you watch the WWDC video for more information.

+2
source

I have a set of UIBackgroundModes keys, and this part works great when writing places to the screen.

Since you are already using background modes, it means the GPS functionality is working properly.

Messages must be sent to the server, even if the application is in the background.

The app takes about 10 minutes of background time and then iOS pauses.

 //Check if device supports multi-tasking if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Get the shared application instance UIApplication *application = [UIApplication sharedApplication]; //Create a task object __block UIBackgroundTaskIdentifier background_task; background_task = [application beginBackgroundTaskWithExpirationHandler: ^ { //Tell the system that we are done with the tasks [application endBackgroundTask: background_task]; //Set the task to be invalid background_task = UIBackgroundTaskInvalid; //System will be shutting down the app at any point in time now }]; //Background tasks require you to use asynchronous tasks dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Perform your tasks that your application requires NSLog(@"\n\nRunning in the background!, here we can send the request to server for 10 minutes."); //End the task so the system knows that you are done with what you need to perform [application endBackgroundTask: background_task]; //Invalidate the background_task background_task = UIBackgroundTaskInvalid; }); } 

Messages should be sent to the server, even if the device is offline, when a message is generated (users will often be outside the cell range, receiving a gps location). Messages do not have to be sent immediately, but they should be stored and sent when the device is reconnected to the network.

For this function, create an HTTP Singleton class and check if the Internet is accessible or not. Also activate the notification of changes on the Internet, so that when you connect to the Internet during use, the application can upload records to the server. To load records on the server, you can either create your own NSURLConnections in GCD to support multiple connections, or you can use the GCDAsyncSocket third-party open source library.

+2
source

First, you must define the background mode in your project in order to perform background operations.

Read this quick tutorial

If you have already taken this step, you can simply add the code to the locationManager callback method. If you want to send data, add a few lines to fulfill the request to the server. You should not often complete requests.

If the request fails, you can store the data in an array or in some kind of queue. Each time you perform a send operation, you can try to send all stored data.

0
source

Source: https://habr.com/ru/post/952787/


All Articles