How to prevent double prompt for geolocation in Phonegap app?

I created a PhoneGap application for iPhone that uses geolocation via JavaScript inside a webview.

When I launch the application for the first time, it will offer me to enable geolocation for this application.

When I click ok, he will again ask me the same question, but this time he states that index.html wants permission to use geolocation.

This makes sense because iOS probably wants to allow geolocation for the first time for the application itself, and the second time the browser wants to get permission.

However, since this does not lead to much user experience:

How can I prevent this double invitation? (It would be enough for me if the 2nd clue could be prevented)

+8
javascript ios iphone cordova geolocation
source share
4 answers

I found the cause of the problem.

A call to navigator.geolocation.getCurrentPosition(onsuccess, onerror) occurs before navigator.geolocation.getCurrentPosition(onsuccess, onerror) is fully loaded.

This means that a webview geolocation request is initiated (rather than a native call via PhoneGap), which again asks for permission (which makes sense). Compare it with the regular Safari browser on your smartphone. He will request geolocation permission for each new website. The same thing happens when loading index.html via PhoneGap when the application starts.

However, the solution is to wait for the deviceready event, which fires when PhoneGap is fully loaded:

 document.addEventListener("deviceready", function(){ navigator.geolocation.getCurrentPosition(onsuccess, onerror, params); }, false); 

This will make the PhoneGap API available, which overwrites the default HTML5 substitution request in the browser and receives the geographic location of the device through its own call (which you already accepted in the first invitation).

This will work because the PhoneGap API calls are identical to the standard W3C calls for HTML5: http://docs.phonegap.com/en/2.2.0/cordova_geolocation_geolocation.md.html#Geolocation

+14
source share

Take a look at this: Location resolution notification on iPhone using PhoneGap

The second seems to be a Webkit warning. To prevent this, you seem to just need to move all your js files to the root directory. Tell me if this works, as I will have to address the same problem soon.

0
source share

Finally, the problem is fixed.

In index.html just move your cordova.js up

<script src = "cordova.js"> </script>

as the first js file to be included (especially make sure it is above the cards, including js). This will ensure that the invitation is displayed only once.

0
source share

I solved this problem by moving

 <script src="cordova.js"></script> 

as the last script to be included

0
source share

All Articles