1

I'm writing this iOS app that contains geo tracking functionality. In my .plist, I have put

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

too make sure the app is not suspended in the background.

The crucial GPS code is in my appDelegate, and looks like this:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{

    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
        isInBackground = YES;
    }

    if (isInBackground) {

        [self sendBackgroundLocationToServer:[locations lastObject]];
    } else {
        // If we're not in the background wait till the GPS is accurate to send it to the server
        if ([[locations lastObject] horizontalAccuracy] < 100.0f) {
            [self sendDataToServer:[locations lastObject]];
        }
    }


}

However, my app stops recording fresh locations to my server after having been in the background for a while.

What could be the cause, and how do I solve this?

Thanks for your advice

Sjakelien
  • 2,255
  • 3
  • 25
  • 43
  • please check this question http://stackoverflow.com/questions/16031120/running-ios-app-in-background-for-more-than-10-minutes?rq=1 – joao.arruda Nov 18 '14 at 10:56
  • thanks, adding yet one more line to the plist did the trick. – Sjakelien Nov 18 '14 at 11:45
  • Actually it DIDN'T! I kept my app running whole night. The app is supposed to send Significant Location Changes when they occur. I drive to the office this morning, but according to my server information, I'm still at home. The app is running in the background now. I'm sure that if I wake it up, it will send my new location to the server. – Sjakelien Nov 19 '14 at 08:19
  • Yes, as expected. Could it be, that is is related to the fact that I have had my Don't Disturb switched on. – Sjakelien Nov 19 '14 at 08:26
  • I'm not really sure, but I think it is possible that, even if your app is appearing when you double click the home button, it might not be running. After a long time in background, if the device needs the memory you are using to run the app, the OS will stop the execution of your app. You will still be able to see it after you double click the home button, but all its data is stored in secondary memory to be recovered when the app gets activated again. – joao.arruda Nov 19 '14 at 12:58
  • Consider trying to send to your server a flag every time your app runs `- (void)applicationWillTerminate:(UIApplication *)application`. I will also try to test if your app still appears in background when this method is called. – joao.arruda Nov 19 '14 at 13:00
  • @joao: That's a good one. Let me try that. It might take a day or so before I get back to you. Now, if the app indeed is terminated, what am I supposed to do next? – Sjakelien Nov 20 '14 at 08:24
  • 1
    I'm not sure. I've heard that if the OS kills your app calling `-(void)applicationWillTerminate:(UIApplication *)application` it disappears from the open apps, but even if it is still there, it might be inactive due to memory usage. Maybe it is not possible to run anything after your app has been killed. Take a look at those: https://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26 http://stackoverflow.com/questions/16031120/running-ios-app-in-background-for-more-than-10-minutes?rq=1 – joao.arruda Nov 25 '14 at 15:57

0 Answers0