thailey01 thailey01 - 19 days ago 18
Android Question

HTML5 Geolocation for iOS and Android

I have an HTML5/jquery-mobile application for cross-platform support. I have Android with WebView and iOS with UIWebView that points to my site. I require geolocation services and while you can pass the device permission from Android to web-app using WebView ChromeClient.

webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback){
callback.invoke(origin, true, false);
}
});


There is no such thing in iOS. Every time I use the application I get two permission request dialogs from the iOS app and the web-app. I don't use phonegap/cordova and have no need for them. This problem has stumped me for weeks and everyone else who has this problem uses phonegap/cordova.

navigator.geolocation.getCurrentPosition(function(position){
// geolocation logic
}


Any help would be much appreciated.
enter image description here

enter image description here

Answer

You can't avoid that alert in UIWebView AFAIK. You could use iOS location API to get current location and pass it to the webpage. Many ways to do this:

  • UIWebView stringByEvaluatingJavaScriptFromString("window.iosLocation = 43.3,-79.9"), on the page can check the UA to see if this it should expect this to be set, or it could wait for 1-2 seconds after load complete and if this prop is not set, then use HTML5 geo. You would inject this on UIWebViewDelegate.webViewDidFinishLoad
  • have your page try call back to iOS native code, although this far more complicated than above (requires js injection/overrides on page load)
  • Set a cookie for your domain on app start that contains the location, then try read the cookie on the page, see here on how to set a cookie: How to manage cookies with UIWebView in Swift
Comments