8

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

When I run the app the first time, it'll prompt me to allow geolocation for this app.

When I hit "ok", it'll prompt me again with the same question but this time it states that "index.html" wants permission to use geolocation.

That makes sense because iOS probably wants permission to allow geolocation for the app itself for the first time and the 2nd time the browser wants permission.

However, since doesn't lead to a great user experience:

How can I prevent this double prompt? (I'd be enough if the 2nd prompt could be prevented)

Timo Ernst
  • 15,243
  • 23
  • 104
  • 165

4 Answers4

14

I found the cause for the issue.

The call to navigator.geolocation.getCurrentPosition(onsuccess, onerror) happens before Phonegap was fully loaded.

This means that the geolocation call of webview (and not a native call via PhoneGap) is being triggered which will again ask for permission (which does make sense). Compare it to the normal Safari browser on your Smartphone. It'll ask for geolocation permission for every new website. It's the same when loading index.html via PhoneGap on application startup.

However, the solution is to wait for the deviceready event which gets fired when PhoneGap has 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 gelocation call of the browser and get the device's geo location via a native call (which you already accepted in the first prompt).

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

Timo Ernst
  • 15,243
  • 23
  • 104
  • 165
  • excellent answer timo. all others should be deleted as they are spreading disinformation. – r3wt Feb 02 '16 at 16:27
0

Have a look at this: Location permission alert on iPhone with PhoneGap

The second one seems to be the Webkit alert. In order to prevent this, you seem to have to simply move all your js files to the root directory. Tell me, if it works since I'll have to address the same issue soon.

Community
  • 1
  • 1
bouscher
  • 1,300
  • 1
  • 9
  • 18
0

Finally fixed the issue.

IN the 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 maps include js). This will make sure that the prompt shows only once

ToughPal
  • 2,231
  • 5
  • 26
  • 30
  • useful, but a conflict occurs, when this script is loaded as u suggested, the web services are unable to load nor giving any error. What's the solution? – developer Mar 02 '15 at 07:35
  • I'm using sencha touch 2, and in Ext.ajax.request that has a conflict with that. how to avoid this? – developer Mar 02 '15 at 07:36
  • This is dangerous, be careful. You might be running into race conditions which work in your case but may not work in other environments (depending on internet speed of the user, browser type and so on...). You should fully read and understand script execution order first: http://www.html5rocks.com/en/tutorials/speed/script-loading/?redirect_from_locale=de – Timo Ernst Jul 14 '15 at 13:26
0

I solved this problem by moving the

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

as the last script to be included

Franz
  • 645
  • 1
  • 9
  • 21
  • This is dangerous, be careful. You might be running into race conditions which work in your case but may not work in other environments (depending on internet speed of the user, browser type and so on...). You should fully read and understand script execution order first: http://www.html5rocks.com/en/tutorials/speed/script-loading/?redirect_from_locale=de – Timo Ernst Jul 14 '15 at 13:26