Solution: PhoneGap, tracking geolocations in the background iOS

16th May 2013

Today I was freaking out, I just started this app and it required the ability to track geolocations, obviously this would need to work in the background if the user got a call, or wanted to do another task on their phone. I searched the web and saw many responses saying it wasn’t possible, but it is!

If you’re using the¬†javascript¬†navigator.geolocation.watchPosition the following worked for me using the latest version of PhoneGap at the time of writing (2.7.0).

Go to the “Resources” folder in your XCode project, click on the file that is called “yourprojectname-Info.plist” (this file may be located elsewhere in other versions of PhoneGap). Click the plus next to “Information Property List”. Type in the new field “Required background modes”, click the arrow located next to the new row and in the value for “Item 0″ type “App registers for location updates”.

Screen Shot 2013-05-16 at 5.40.41 PM

That should do the trick.

Categories: PhoneGap

Tags: iOS, PhoneGap


« Back

Comments

  1. Greg says:

    Hi, does this mean your PhoneGap app, is active in the background and receives location change events? Even if the phone is locked?!

  2. That’s correct. It works perfectly most of the time. The only thing I’ve found is if you are stationary for 10 minutes or so it will stop making calls. I’m going to test ways of re-initiating it but haven’t done so yet.

  3. Greg says:

    Just tested it on a simulator and it does indeed seem to continue to write to the console that the location has changed. Going to have to see about that 10 minute timeout thing now though

  4. Ben says:

    I haven’t been able to get this to work. I did as you mentioned, yet when I press the home button in the simulator, it no longer writes to console until I bring the app back up. Suggestions? I’m using PhoneGap 2.6.0 and iOS 6.

  5. Scott says:

    Did you guys have any luck getting past the 10-minute-ish barrier? I’m stuck there now. My app keeps running, but does not receive any location updates after that time.

  6. @Scott, I haven’t had any luck on this yet, do let us know if you find a solution. I will let you know if I find one also.

  7. @Ben, the only difference I could say is maybe it doesn’t work in 2.6.0 – A long shot, but I know it works on 2.7.0 on iOS6 and iOS7 based on the tests that I have done.

  8. jose luis says:

    hello
    you will never cross the 10 minutes barrier
    please check:

    http://www.macworld.com/article/1164616/how_ios_multitasking_really_works.html

  9. Angeltveit says:

    @jose luis excuse me, but your article states the following:

    “There are exactly five kinds of apps allowed to run indefinitely in the Background (…)

    Apps that track your location in the background. For example, you still want voice prompts from your turn-by-turn GPS navigation app, even if another app is Active.”

  10. Chris Scott says:

    Try my Cordova background-geolocation plugin for iOS and Android
    https://github.com/christocracy/cordova-plugin-background-geolocation

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>