4

I have read some of the discussions on geolocation in background on PhoneGap, but am still having problems.

I'm working on an application that uses geolocation services. On IOS when application is in foreground all works fine. When the application goes to background (after around 30-40 minutes) however the geocoding stops working and I don't get the change position event anymore. During the period in background the application successfully JSON's a time stamp and lng/lat every few seconds to my remote server so I know it's working during that limited period.

My configuration is:

Frame work: PhoneGap 2.5

Devices: iOS (tested on 4, 4s & 5 iPhone & iPad4)

iOS Version: 7.0.4

Xcode: Version 5.0.2 (5A3005)

Plugin: PowerManagement Plugin for Cordova (2.0+) by Wolfgang Koller - GOFG Sports Computer https://github.com/purplecabbage/phonegap-plugins/tree/master/iOS/PowerManagement

startWatch Function:

function onDeviceReady() {
    startWatch();
    function startWatch() {
        var options = { enableHighAccuracy: true, timeout: 3100, maximumAge: 0 }; 
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);            
        timerID = setInterval ( updateLocation, 3000 ); 
    }
.
.

App-info plist Includes:

"App registers for location updates" under "Required background modes"
&
<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

config.xml includes:

<plugin name="PowerManagement" value="PowerManagement" />

Questions:

I was wondering is there a way to ensure JavaScript in PhoneGap runs continually when the app is in the background?

Do I need to set Wakelock acquired/ release to ensure the Powermanagement plugin work efficiently in iOS? Could someone suggest a different plugin maybe or config/code I may have missed?

Cheers, Wayne Gardner Sydney Australia

Enyawg
  • 51
  • 1
  • 4
  • A large number of applications rely on background threads to provide a smooth user experience: calculating the GPS positions in the background, for example. PhoneGap APIs are built using JavaScript which is not multi-threaded and hence do not support background processing. – Nijil Nair Nov 21 '13 at 05:04

2 Answers2

5

Try out mine. Works great for iOS and Android

The plugin has a debug mode where it emits sounds for its life-cycle events (location change, exit stationary-region, and more) which is great for getting instant feedback while field-testing.

https://github.com/transistorsoft/cordova-background-geolocation-lt

Christocracy
  • 103
  • 1
  • 7
christocracy
  • 51
  • 2
  • 5
2

I'm not sure but maybe IOS is like Android for this function : Creating an Android Service with Phonegap? (Have phonegap app run even when closed)

So, Javascript will stop when app goes in background or a few sec(or min) after. Moreover, PowerManagement isn't the solution. PowerManagement allows you to tell the OS : "Hey guy, I have an which need to run even if the phone is lock, so please don't turn off the CPU (or Screen) when I lock the phone.

Maybe try this (only for IOS > 5 and WP8) : https://github.com/katzer/cordova-plugin-background-mode I'm a IOS pro, I'm an Android Pro and I use Cordova and this plugin (PowerManagement) and it doesn't do what you think.

I hope I help you.

Coco

Community
  • 1
  • 1
Cocorico
  • 1,998
  • 1
  • 22
  • 38