Background Mode location is not working on iPhone

18 posts, 0 answers
  1. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 11 Jun 2013 Link to this post

    I am writing a location geo-fencing app to keep track of driver stops at specifics locations.  Everything works, except when I hit the home key and put the app in background mode, the location arrow at the top of the phone stays on for about 10 seconds and then goes off, and of course does not log the coordinates.  Same problem if the phone goes to sleep after less than a minute … which makes the app totally useless.  I have gone to properties. iOS->Background Mode-> and select “Location” as on (it now has a blue background).  Is there anything else I can do? 
  2. David Silveria
    David Silveria avatar
    234 posts
    Member since:
    May 2006

    Posted 11 Jun 2013 Link to this post

    Background modes work only when you deploy directly on device, they would not work if you deploy via ION.
  3. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 11 Jun 2013 Link to this post

    Thank you!
  4. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 11 Jun 2013 Link to this post

    I deployed my app to my ipHONE by generating the csr file from Icenium and then from Apple's Dev site I create the ios_development.cer and then the profile mobile provision. I was able to Run on device.  But I was NOT able to Publish (see attached screen shot).  But since I can run on device, I am still having the problem with the location arroe turning off after about 10 seconds ... which is the origianl issue.
  5. Jan-Dirk
    Jan-Dirk avatar
    327 posts
    Member since:
    Jun 2012

    Posted 12 Jun 2013 Link to this post

    Click 'properties' in the project explorer and check the value for the application identifier. Does that say 'com.telerik.DriveLog'? Is that what you used in the provisioning too?
  6. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 12 Jun 2013 Link to this post

    Hi Gary,

    About your last post - Jan-Dirk is right, you do not have provision for this App ID, for more information refer to:
    As for the original issue - for test purpose I've modified the sample-geolocation demo because it is already using the watchPosition method which is required to start the location tracking. Then I've hooked the pause and resume Cordova events exactly as documented to see when the app would be paused/suspended. A console.log in the success callback of the watchPosition method shows us whether the background service keeps working after the phone is suspended. This examples works on our end on iPhone 5 with OS 6.1.3. I've attached the modified sample as per the above instructions - you can use it to test whether it works on your end.

    If you have deployed it on your iOS device with development provision and continue to have problems, can you share what is your device and iOS version, so we can test locally?

    Regards,
    Steve
    Telerik

    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Missed our first webinar, watch it here.
    Share feedback and vote for features on our Feedback Portal.
  7. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 12 Jun 2013 Link to this post

    Steve, thank you, that was an awesome example.  I got mine to work and it continued to work as expected ... until it stopped.  I suspect I walked into an area with no cell coverage to it stopped and never started up again. 

    Any suggestions on how I can automatically check if it is on and restart it if not (while in the background)?  Since I really only need it to check every 10 or 15 seconds, I tried putting a javascript timer and then calling geolocation.getCurrentPosition instead of the geolocation.watchPosition but it appears (not fully confirmed), that the javascript timer goes to sleep in the background so this is of no use.  I also tried setting the frequency  and maximumAge but it seems to ignore the setting and still updates every 1 second.
  8. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 13 Jun 2013 Link to this post

    Hello Gary,

    iOS Location Services use information from cellular, Wi-Fi, and Global Positioning System (GPS) networks to determine your approximate location. Based on that, your app should not have access to any of those in order to stop reporting position. This does not mean the location service is not running, it means it cannot determine whether your position has changed and that is why it does not report anything, as by default it only reports position when significant changes in position occur.

    Also bear in mind the following paragraph from Apple's documentation:
    The inclusion of this value in the UIBackgroundModes key does not preclude the system from suspending the app, but it does tell the system that it should wake up the app whenever there is new location data to deliver. Thus, this key effectively lets the app run in the background to process location updates whenever they occur.

    The only background service that is never suspended is voip.

    Regards,
    Steve
    Telerik

    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Missed our first webinar, watch it here.
    Share feedback and vote for features on our Feedback Portal.
  9. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 17 Jun 2013 Link to this post

    I must be doing something wrong.  I am testing with an iPhone 4s and I have tried lots of things and the end result is that the little arrow indicating that location services are active  always goes off within 30 minutes and never comes back on (no matter how much I move).  If someone wanted to use a location tracking log application, then it would have to be very dependable and always work.  Is there something I am missing?
  10. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 19 Jun 2013 Link to this post

    .. changed this post to be a reply to Steve instead.
  11. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 19 Jun 2013 Link to this post

    I believe the secret to life is asking the right question, so I might be asking it wrong so please adivse if I need to ask it differently. But my problem is that my location monitor app stops monitoring after less than an hour (the location arrow icon on my iPhone goes off by it self). The cause of the problem is not known, so I guess the right question is what is causing my app to stop monitoring? I know as soon as I click it from the running apps icon tray it starts working again. I log errors but there are none (I assume).

    Or is the right question how do I keep the location icon on all of the time?

    Steve, is it possible that you can load your sample app onto your iPhone, start the Geolocation watch and then minimize it and check again in an hour or so to see if the location icon goes off?
  12. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 20 Jun 2013 Link to this post

    Hello Gary,

    I completely agree that asking gets us forward and there is nothing wrong in your question. The thing is we do not add anything to this process, we simply include the necessary UIBackgroundModes for your app into the info.plist file as required by Apple and from there on, it is their show. My point in the previous post was that real testing of the location background mode would include you moving with your iOS device so that it detects significant changes in location. If I read Apple documentation right, if your device just sits on your desk, there are no location changes and it would be suspended after some amount of time. If you believe there is a problem with this service, I would suggest contacting the guys at Apple or Cordova for an expert view on this.

    Regards,
    Steve
    Telerik

    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Missed our first webinar, watch it here.
    Share feedback and vote for features on our Feedback Portal.
  13. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 20 Jun 2013 Link to this post

    A couple of points of clarification:  My app is intended to log activity of where a driver has been thru out the day.  it would be normal for them to be in a single place for an hour without moving.  The location arrow going off is not a problem ... as long as it comes back on when they do move.  It does not. 

    So based on what you have said, is there something I can do to keep a constant poll going so to keep the location service active?
  14. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 21 Jun 2013 Link to this post

    Hello Gary,

    We appreciate the context of your app and understand your concerns. To answer your question directly, no you do not have any additional control over apple's location background mode. I would suggest reviewing the following articles on apple's site that elaborate on it:

    if it does not work as you would expect, then you might consider using the significant-change location service instead (which by the way is recommended by Apple whenever you do not need high-precision location data):


    Regards,
    Steve
    Telerik

    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Missed our first webinar, watch it here.
    Share feedback and vote for features on our Feedback Portal.
  15. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 22 Jun 2013 Link to this post

    Apple's "significant-change location" sounds like exactly what I need.  But how do I access it from my Icenium project? navigator.geolocation services is an HMTL5 thing and startMonitoringSignificantLocationChanges (significant-change location) is an IOS4 thing.
  16. Yavor Georgiev
    Admin
    Yavor Georgiev avatar
    982 posts

    Posted 26 Jun 2013 Link to this post

    Hi Gary,

     Let me try shedding some light on iOS location services. -[CLLocationManager startMonitoringSignificantLocationChanges] isn't implemented in the Cordova APIs. There's a plugin that exposes the API to JavaScript, but it's not Cordova Plugman-compatible and so you can't use it in Icenium without some rewrite of the plugin itself.

     There are a couple of differences between startUpdatingLocation (the method used by Cordova on iOS) and startMonitoringSignificantLocationChanges. startUpdatingLocation
    keeps the GPS sensor on until stopUpdatingLocation is called. It can report location changes with a degree of accuracy specified by the caller. If you build your app with the location background mode enabled, call navigator.geolocation.startWatch and then switch away from your app, iOS will forcefully set the accuracy on the location manager to the lowest setting. What's more, the system can decide to suspend your app and resume it only to report a location change and then suspend it again a short while after. While your app is in the background, active or not, iOS decides how to manage the GPS sensor and may even turn it off for a while according to their power management rules, and only poll it periodically to check for changes.

     On the other hand, an app using startMonitoringSignificantLocationChanges doesn't need the location background mode. This API allows for the app to be killed by the OS, not only suspended. Calling this method tells the OS that it should launch your app in the background whenever it detects a significant change in location, and then kills it again unless the app requests an extension (which can last up to 10 minutes) to do network requests or other IO with the new information. However, the GPS radio doesn't come into play here - rather, the device relies on cell towers and WiFi hotspots to decide if its location has moved significantly. Depending on cell infrastructure, this can provide a wildly varying degree of accuracy. Only when the system wakes your app can it poll the GPS.

     All that being said, if you use the background location mode and have called navigator.geolocation.startWatch, your app should be resumed and the startWatch callback invoked once the system detects a significant change in location. Again, depending on circumstance, exactly when this happens can vary. You can try cheating iOS by connecting the device to a car lighter charger - iOS is much more liberal with background services when it's connected to a power source.

     However, if that doesn't work out for you, there are a couple of things you can do - you can either experiment with the plugin I linked to in the beginning of my post, or you can try another plugin that will disable the device's auto-lock, as long as the app is in the foreground. This will make sure your app never goes in the background unless the user presses the Home or Power buttons on the device or otherwise switches away from your app.

     The folks on the Apple Developer Forums and the Cordova forum might know more about this, so you might want to check those out also.

    Regards,
    Yavor Georgiev
    Telerik

    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Missed our first webinar, watch it here.
    Share feedback and vote for features on our Feedback Portal.
  17. Gary Hoehn
    Gary Hoehn avatar
    10 posts
    Member since:
    May 2009

    Posted 26 Jun 2013 Link to this post

    Yavor, Excellent post, thank you!

     

    You stated one thing that gives me hope in resolving my issue: What's more, the system can decide to suspend your app and resume it only to report a location change and then suspend it again a short while after. While your app is in the background, active or not, iOS decides how to manage the GPS sensor and may even turn it off for a while according to their power management rules, and only poll it periodically to check for changes.

     

    Correct me if I'm wrong, but I assume from this then that the when the GPS sensor goes off, the navigator.geolocation stops watching.  So I guess the right question is how can I do a timer and poll the GPS every minute instead?  Will a regular JavaScript setInterval loop stay active in background mode or is there a better way to setup a timer to do something on a regular basis?

     

     

  18. Yavor Georgiev
    Admin
    Yavor Georgiev avatar
    982 posts

    Posted 26 Jun 2013 Link to this post

    Hello Gary,

     When the app is suspended, any watches created by navigator.geolocation.startWatch are preserved. Whenever iOS decides to resume your app and activate the GPS sensor, it'll call -[CLLocationManagerDelegate locationManager:didUpdateLocations:] which Cordova's native geolocation code on iOS implements. Cordova's CDVLocation class on iOS (which is exposed as navigator.geolocation in JS) keeps a list of all the watches created by startWatch and whenever iOS calls the locationsChanged callback, even when resuming the app in the background, all the watches will be signaled. In essence, navigator.geolocation.startWatch survives backgrounding and GPS availability, except in cases where the error callback is called. Once the error callback of a watch is invoked, the watch is considered broken, geolocation stops, and the app should call startWatch again or fail gracefully. By the way, is it possible that the error callback on your watches is invoked?

     Even if you could periodically execute a function while in the background (and iOS doesn't do this, neither in JavaScript, nor in Objective-C. Only voip apps are guaranteed to be active in the background all the time), subsequent calls to navigator.geolocation.startWatch won't force turning on the GPS sensor if it's on standby because of the way Cordova's CDVLocation and Apple's CLLocationManager.

    Regards,
    Yavor Georgiev
    Telerik

    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Missed our first webinar, watch it here.
    Share feedback and vote for features on our Feedback Portal.
Back to Top