Reputation: 7174
I have an application that is making use of the PhoneGap (version 1.4.1). It is not a normal PhoneGap application as the application has been built with MonoTouch, not Xcode. The PhoneGap library is being loaded into a IUWebView and the phonegap-1.4.1.js file is located on a remote server. I can call the functions in the PhoneGap API and everything works fine; the only problem I have is with the PhoneGap Geolocation API. Although geolocation works, everytime the webpage is loaded a request for permission is made to use the iPhone locations services, but it does not request permission for my application name, it requests permission for the URL where the page source is located. So the alert will say "http://xxx.xxx.xxx.xxx would like to use your current location" and I the user answers "Allow", the iphone does not remember this because it will ask again the next time the page is reloaded.
Does anyone know why my call to the PhoneGap geolocation API is doing this? And is some way for me to get PhoneGap to request permission for my application to use location services, rather than the URL?
Upvotes: 1
Views: 3179
Reputation: 7174
I found the solution to this problem. PhoneGap does a replacement of all UIWebView/MobileSafari navigator.geolocation functions with its own functions so that a call to a navigator.geolocation function will execute the corresponding PhoneGap function. To see where this is happening in the javascript source code, search for the following line:
if (typeof navigator._geo == "undefined")
The description directly under this line describes the problem outlined in this question that PhoneGap is trying to avoid. The main problem, though, was that I was executing the following in jquery's document.ready handler:
navigator.geolocation.getCurrentPosition(onGpsSuccess, onGpsError, { enableHighAccuracy: true });
and at this point in the execution the PhoneGap initialization had not yet been completed, so the call to navigator.geolocation.getCurrentPosition called MobileSafari's version of getCurrentPosition, not PhoneGap's. I determined that the initialization was not completed by putting alert(navigator._geo); in document.ready and found that it returned undefined.
The solution then was to replace the line of code in the document.ready with the following:
document.addEventListener("deviceready", function() {
navigator.geolocation.getCurrentPosition(onGpsSuccess, onGpsError, { enableHighAccuracy: true });
}, false);
This ensures that navigator.geolocation is only called when PhoneGap has completed its initialization.
So, if you encounter this problem, then check the following:
Upvotes: 2
Reputation: 3703
That's because PhoneGap uses WebViews. And like Safari, the webview could load any URL so it must ask on a URL by URL basis.
Upvotes: 0