navigator.compass not working in iOS

  • 2
  • Problem
  • Updated 9 years ago
  • Solved
I am building using build.phonegap.com and can't get this to work:

if(navigator.compass){ navigator.compass.getCurrentHeading(compasssuccess, fail); navigator.compass.watchHeading(compasssuccess, fail, {frequency: 200});
}

navigator.compass is undefined.

The Android build does it fine.
Photo of JDB

JDB

  • 4 Posts
  • 0 Reply Likes
  • sad

Posted 9 years ago

  • 2
Photo of Juan Diego

Juan Diego

  • 33 Posts
  • 0 Reply Likes
I found this WEIRD behavior:

Compass works when I rename the phonegap.0.9.6.js to phonegap.js reference (and the included js file as well).

How can that change the behavior? which version does the build service use by default?
Photo of Andrew Lunny

Andrew Lunny

  • 1911 Posts
  • 199 Reply Likes
There's been a lot of confusion about how phonegap.js works with PhoneGap Build - I will try to write something up this week to explain how it works.

Each PhoneGap platform has its own version of phonegap.js (each of which is identified with a particular PhoneGap release). These copies are not compatible with one another - you cannot use phonegap.js from phonegap-iphone on an Android device, for instance.

To get around this, PhoneGap Build inserts a phonegap.js file into your builds at build time - one specific for each platform. This file is tied to the native release of PhoneGap we build with - at present, 0.9.5, until some issues are ironed out with the 1.0 release candidate.

To use it, just insert a script tag referencing phonegap.js. If you reference phonegap.0.9.x.js in your index.html, you might get lucky and it will work on a single platform; or, as in your case, there'll be a conflict between the web view (0.9.5) and the JavaScript (0.9.6), and it will fail to work.

Hope that clears it up a bit.
Photo of Juan Diego

Juan Diego

  • 33 Posts
  • 0 Reply Likes
Ok that explains. I was uploading and referencing the Android phonegap.0.9.6.js thinking it was mutiplatform. So now I just use phonegap.js and its fine.

The only issue I currently have is this one which I believe is due to the 0.9.5 and 0.9.6 mismatch? Is there any ETA for the fix?

Thanks
Photo of Andrew Lunny

Andrew Lunny

  • 1911 Posts
  • 199 Reply Likes
We will update the PhoneGap versions later today.
Photo of Juan Diego

Juan Diego

  • 33 Posts
  • 0 Reply Likes
Localization suddenly stopped working on builds in the last 4 hours. Maybe the update has caused it?
Photo of Juan Diego

Juan Diego

  • 33 Posts
  • 0 Reply Likes
I can confirm this: http://community.phonegap.com/nitobi/...

Now neither GPS and Compass work when referencing Phonegap.js. If I try it with the Android phonegap.0.9.6.js the GPS works. Shouldn't be the other way around?
Photo of Hardeep Shoker

Hardeep Shoker

  • 1941 Posts
  • 89 Reply Likes
Hello Juan,

The issue has been confirmed. We are currently looking into resolving it.

Hardeep Shoker
Photo of Hardeep Shoker

Hardeep Shoker

  • 1941 Posts
  • 89 Reply Likes
Alright the issue has been resolved and the api calls are now operational.

Hardeep Shoker
Photo of Juan Diego

Juan Diego

  • 33 Posts
  • 0 Reply Likes
Hi again. My app relies heavily on the compass and I have noted (since the last 2 weeks more or less) that compass randomly stops reporting activity.

It is just random and even restarting the app (window.location='index.htm') won't help, only phone reset gets it back. Other apps using compass are not affected so its isolated to Phonegap.

Can you please check?
Photo of Andrew Lunny

Andrew Lunny

  • 1911 Posts
  • 199 Reply Likes
What happens if you restart the app - double tap to bring up the list of open applications, then press and hold until a red minus sign appears at the top left of the app's icon. Press the minus sign to close the app.

Is the compass responsive again once you reopen the app at this point (is this what you mean by phone reset)?
Photo of Juan Diego

Juan Diego

  • 33 Posts
  • 0 Reply Likes
I didn't know it was possible to kill apps on iPhone, so I was just powering off and on (aka reset).
Will try killing the app however I think should give same result.

For more detail, this is how I'm using the compass:

function onDeviceReady() {
if (navigator.compass)
{ navigator.compass.getCurrentHeading(compasssuccess, fail);
navigator.compass.watchHeading(compasssuccess, fail, { frequency: 200 });
}
}

Something is getting stuck in Phonegap, something that goes beyond the scope of the app code (because reloading the page does not clear it)
Photo of Andrew Lunny

Andrew Lunny

  • 1911 Posts
  • 199 Reply Likes
It might be an issue with the compass timing out; there's a related issue on the PhoneGap iPhone project here:
https://github.com/phonegap/phonegap-...

Can you add some comments to that issue? It might help the PhoneGap iPhone devs get a better handler on the problem.