18 Replies
      Latest reply on Nov 21, 2019 1:37 AM by PK17
      karela Level 1 Level 1 (0 points)

        I am writing an iOS app that requires continuous location updates while being in the background. I am using CLLocationManager and here is what I am doing:

         

        1) Requesting always authorization

        2) Enabling background location in info.plist

        3) Setting allowsBackgroundLocationUpdates to true

        4) Starting location updates while in the foreground

         

        The app gets location updates when in foreground. App also continues to get location updates in background every 1 second, until some time when the app gets suspended and does not get any further location updates. When the app gets suspended varies from anywhere between few 10s of minutes to hours. I have even noticed that the app sometimes does not get suspended but stops getting location updates.

         

        My question is: is there a way to continuously get location updates indefinitely (or say for 24 hours) with the app in the background and not getting suspended. And if the app gets suspended, is there a way to wake up the app and resume getting continuous location updates without the user bringing the app to the foreground?

         

        I have tried significantLocationUpdate to wake up the app and restart my location manager. With this I am able to get significantLocationUpdates for days but I get location updates about once every 5 minutes (while driving) but the location manager that I create and start to get continous frequent locations does not return any locations. I would like to get it more frequently, like once every few seconds.

         

        Any help would be much appreciated.

         

        Thanks.

        • Re: Continuous location in background or after suspension
          NotMyName Level 4 Level 4 (875 points)

          You need to deal with two important facts:

          1.  Your app will get suspended and resumed.

          2.  Your app will get suspended, terminated, and relaunched by the system.

          and iOS is going to do either or both of those things between location updates.

           

          If you don't handle both of those cases correctly, then your app is going to stop receiving updates when you hit the case you're not dealing with right.  The most likely case is that you're not handling the location key properly.

          • Re: Continuous location in background or after suspension
            KMT Level 9 Level 9 (15,275 points)

                 > I would like to get it more frequently, like once every few seconds.

             

            Which is more important to your app? Update frequency or accuracy?

             

            As you note, sLUs are the result of course grain changes.  Does the app track and map a travelled route or does it provide turn-by-turn navigation, or...?

             

            If nav, you should instead be using kCLLocationAccuracyBestForNavigation

             

            It sounds to me tho, that your app is a tracker, in which case you need to focus on energy use first, I think, then take what the system provides in terms of accuracy

             

            See:

            - kCLLocationAccuracyBestForNavigation

             

            - https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/LocationBestPractices.html

            Request Quick Location Updates

            If your app just needs a quick fix on the user’s location, it’s best to call the requestLocation method of the location manager object, as shown in Listing 14-1. Doing so automatically stops location services once the request has been fulfilled, letting location hardware power down if not being used elsewhere. Location updates requested in this manner are delivered by a callback to the locationManager:didUpdateLocations: delegate method, which you must implement in your app.

              • Re: Continuous location in background or after suspension
                karela Level 1 Level 1 (0 points)

                Which is more important to your app? Update frequency or accuracy?

                 

                -- Both are important but I am willing to settle for lower accuracy in order to continue getting location updates.

                 

                -- In my description above of what I am doing, I am using kCLLocationAccuracyBestForNavigation and getting location updates as I like, but not for long after the app goes to background. I thought that this could have to do with power consumotion and tried to reduce accuracy when no major change in location was detected so that the app does not get suspended but that did not help either.

                 

                -- I was using significant location change as a last resort as I could not find another way to wake up my app.

                 

                In principle, is continuous location update (e.g. kCLLocationAccuracyBestForNavigation with startUpdatingLocation) supposed to keep the app running in the background or wake it up if suspended? If so, then what is it that I am missing?

                  • Re: Continuous location in background or after suspension
                    karela Level 1 Level 1 (0 points)

                    One more question: Is it known how frequently does significant location change wake up the app (in terms of distance and/or time) -- what's the best I can expect from it? From my tests, I have seen every 5 minutes and one of Apple's documentation said "it wakes the system and your app at least every 15 minutes, even if no location changes have occurred," but it did not say what's the most frequent it would do the same if I am driving and location is fast changing.

                      • Re: Continuous location in background or after suspension
                        KMT Level 9 Level 9 (15,275 points)

                             >but it did not say what's the most frequent

                         

                        There is no max spec, only minimum. How frequent varies and depends on hardware, what other apps are doing on the device (any others doing something similar w/location?), reliability of signal, etc. You can run a timer to check every n seconds or minutes, etc, but there is no guarantee it will be satisfied every time, on time.

                         

                        You shoild build your app and field test to see what times are realistic for it.

                      • Re: Continuous location in background or after suspension
                        KMT Level 9 Level 9 (15,275 points)

                        Let's do the basics... (sorry if you mentioned and I missed them):

                         

                        - has your app registered required background modes... set to "App registers for location updates".

                         

                        - have you checked backgroundRefreshStatus? Is this app allowed to run in the background according to it?

                         

                        - what is the value of pausesLocationUpdatesAutomatically?

                         

                        - what accuracy are you requesting? Are you moving sufficiently to generate changes over that accuracy?

                         

                        - Did you call startUpdatingLocation?

                          • Re: Continuous location in background or after suspension
                            karela Level 1 Level 1 (0 points)

                            - has your app registered required background modes... set to "App registers for location updates".

                            -- Yes

                             

                            - have you checked backgroundRefreshStatus? Is this app allowed to run in the background according to it?

                            -- Yes, it's Avalilble (although I do not do anything specific to set that and I do not see my app under Background App Refresh under Settings on the phone)

                             

                            - what is the value of pausesLocationUpdatesAutomatically?

                            -- I do not set that -- default is supposed to be true.

                             

                            - what accuracy are you requesting? Are you moving sufficiently to generate changes over that accuracy?

                            -- I am requesting bestForNavigation. The app is supposed to work in car, so I assume yes. But I am not lowering it when the drive finishes (althouh I hve tried that too)

                             

                            - Did you call startUpdatingLocation?

                            -- Yes

                             

                            Like I said, I am getting location updates in the background, just that it eventually stops.

                             

                            Thanks

                              • Re: Continuous location in background or after suspension
                                KMT Level 9 Level 9 (15,275 points)

                                Try:

                                 

                                locationManager.pausesLocationUpdatesAutomatically = false

                                 

                                I should note my expectation is that after making that change, you perform an option-clean build folder before you build and then take another test long drive. Good luck.

                                  • Re: Continuous location in background or after suspension
                                    karela Level 1 Level 1 (0 points)

                                    I had tried setting pausesLocationUpdatesAutomatically = false but that did not help -- the app was getting suspended after a while and no further location updates were coming. However, now I have a version where I set this to false, along with extra code to automatically adjust location accuracy depending on whether driving is detected. This seems to be working so far. I get continuous location updates in background and the app has not been suspended / terminated for the last couple of days.

                                     

                                    Basically, this is what I am doing: 1) Start off with low accuracy location updates (KM accuracy, which uses only cell tower, no GPS chip)

                                    2) When driving is detected (based on change in location), increase location accuracy to bestForNavigation

                                    3) When driving seems to have ceased (based on speed or change in location), reduce location accuracy back to KM

                                     

                                    Thanks for all your help. Let's hope this keeps working and doesn't suspend or terminate the app. Although I am still interested in knowing how can I wake up the app and resume location tracking after the app has been suspended or terminated.

                                    • Re: Continuous location in background or after suspension
                                      om123 Level 1 Level 1 (0 points)

                                      Hello,

                                       

                                      How can i resumed the location update when app is killed from app switcher of iphone ?

                                      MyApp working fine in Foreground and background.

                                      But whenever user killed the app, the location updated get stop.

                                       

                                      i  set  "locationManager.pausesLocationUpdatesAutomatically = true " because i want to update the location when user is moving not when user is steady.

                                      and  also i set all the location desired accuracy depend on user speed.

                                       

                                      Same i debug in Xcode's simulator,

                                      i added gpx file in my project.

                                      Start location update in foreground and location is updating-> working fine

                                      now move the app in background and location is updating -> working fine

                                      now i killed the app from app switcher of simulator and location is updating -> working fine in simulator but not in actual device.

                                       

                                      Whenever user killed the app, the location update get stop in actual device. Is there any way to resume the app for location update when user killed the app?

                                       

                                      Thanx

                            • Re: Continuous location in background or after suspension
                              karela Level 1 Level 1 (0 points)

                              Happy to report that I finally got it working. The app has been capturing my trips as expected in the background for 5 days straight now. The key was to detect driving and only then increase the frquency of location updates (to use GPS chip), along with setting locationManager.pausesLocationUpdatesAutomatically to false (as suggested by KMT -- thanks!!)

                               

                              1) Start off with low accuracy location updates (KM accuracy, which uses only cell tower, no GPS chip)

                              2) When driving is detected (based on change in location), increase location accuracy to bestForNavigation

                              3) When driving seems to have ceased (based on speed or change in location), reduce location accuracy back to KM

                              4) At all times, keep locationManager.pausesLocationUpdatesAutomatically set to false

                               

                              Hope this helps others too!

                                • Re: Continuous location in background or after suspension
                                  ocanal Level 1 Level 1 (0 points)

                                  Hi karela,

                                  It would be great if you shared some code for step 2 and step 3 that how you detect driving or ceased?

                                  I have the same problem with you and I've just despaired than I saw your question and you solved that makes me happy?

                                  I will try to achive this problem myself,

                                   

                                  But as I said it would be greate if you share some parts.

                                   

                                  Thanks.

                                  • Re: Continuous location in background or after suspension
                                    Scott--R Level 1 Level 1 (0 points)

                                    Karela is this solution still working for you in the latest versions of iOS???

                                    • Re: Continuous location in background or after suspension
                                      sanchit.mittal@hotcocoasoftwar Level 1 Level 1 (0 points)

                                      Hi Karela,

                                      I also have very similar problem statement to get location updates after every few seconds irrespective of the activity of an individual, what I observed while testing my code is that the app is giving location updates in background mode for hours and sometimes 1-2 days but then stops. My use case is to get location updates until the user kills the app. Also, I have used standard location update as I need updates after every few seconds so can't wait for a significant update. In implementation thing, I have done quite similar things

                                       

                                      [self.locationManager setAllowsBackgroundLocationUpdates:YES];

                                      [self.locationManager disallowDeferredLocationUpdates];

                                      [self.locationManager setPausesLocationUpdatesAutomatically:NO];

                                       

                                      self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

                                      self.locationManager.distanceFilter = 0;

                                      [self.locationManager requestLocation];


                                      Can you throw some light over this, also I think since Apple allows background modes to get location updates and if the phone has sufficient memory, then it shouldn't be terminating the app. What do you think that why your app worked by changing desired accuracy (depending on activity)


                                      Any help would be highly appreciated.

                                      • Re: Continuous location in background or after suspension
                                        indra01tiwari Level 1 Level 1 (0 points)

                                        Hi Karela,

                                         

                                        Could you please share the code that you used to get location event in background. I am developing similar app where we need continuous location update even in background. We have used following methods to get the location.

                                        1. Get the permission from user for always allow location.

                                        2. Added permission in plist

                                        3, Enabled location in background capability.

                                        4. Added geofencing for monitoring.

                                        5. [self.locationManager setPausesLocationUpdatesAutomatically:NO];

                                         

                                        After all this above mentioned steps we are not able to get locations after some time. Any help will be appreciated. We dont want to use monitor significant changes as we want location update after every certain intervals with good accuracy.

                                         

                                        Please let me know what can we to make our app work in background continuously.

                                         

                                        P.S. :-  Our app also requires the location update even if app is killed by user.

                                         

                                        Thanks in advance. :-)

                                        • Re: Continuous location in background or after suspension
                                          PK17 Level 1 Level 1 (0 points)

                                          Hi Karela,

                                          I do have the same problem. I have been trying from many days. Could you please share the code so that it will helpful to me.