Search code examples
javascriptioscordovaaccelerometernavigator

Trying to call navigator.accelerometer in Phonegap


Okay, so I am learning Phongap right now by going through examples. I have about 7-8 months of javascript exposure/experience, but I am completely new to xCode and Phonegap (about a week and a half maybe). I don't feel that this effect my issue right now.

I don't want to out and ask, "Does navigator.accelerometer exist?", but that almost seems like the question right now. I 'know' that is does exist, because its called everywhere in tons of examples, but when I actually try to call it, I get errors about it being undefined. I am doing this, but to no avail:

var foo = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

When I call navigator.accelerometer I get "'undefined' is not an object".

I have Phonegap installed and running when I compile, so I have no more ideas about what the problem could be. I am assuming that this is not a deprecated property or syntax, and based on what I see in W3C schools, I have conclude (possibly ignorantly) that accelerometer is a property added to navigator by Phonegap. Help is greatly appreciated. Thanks


Solution

  • Go to your config.xml and add

    <plugin name="Accelerometer" value="CDVAccelerometer" />
    

    in the <plugins> section. Or, if you use cordova 3.0+

    <feature name="Accelerometer">
          <param name="ios-package" value="CDVAccelerometer" />
    </feature>