2

In your standard desktop browser like Safari or Chrome, when you navigate to a site such as maps.google.com and click on the 'my location' button, a message appears asking you if you'd like to give permission to the website to use your GPS/wifi triangulation position.

My question is, how do you detect such an event and show a dialog to a user when using a standard Cocoa (desktop) WebView object? I've tried implementing all of the various delegate methods and none of them seem to catch this javascript event.

navigator.geolocation.getCurrentPosition(function(position) {
  do_something(position.coords.latitude, position.coords.longitude);
});

It just fails silently. How do I ask the user for location sharing permission?

Andrew
  • 2,690
  • 23
  • 27
  • 2
    Did you see [this question on Stack Overflow][1]? It looks like that could be a solution. [1]: http://stackoverflow.com/questions/5505617/how-can-i-set-my-latitude-and-longitude-for-debugging-the-geolocation-api-with-g – Kristof Van Landschoot Dec 15 '11 at 11:23
  • I'm marking your answer correct, but it is the comment that led me down the right path. I still have plenty of questions, but at least now I can intercept the geolocation js request and do something about it in Obj-C. – Andrew Dec 15 '11 at 14:27
  • I tried adding my comment as a new answer, but StackOverflow automatically converted it to a comment. Anyway, thanks! Glad I could help. – Kristof Van Landschoot Dec 16 '11 at 12:18
  • @andrew, can you please let me know how you were able to intercept the call? I need to make it work in web view for my app . – Sagrian Sep 17 '13 at 10:42
  • @Sagrian, It was a client project and I no longer have the source. I think you have to rewrite the javascript function getCurrentPosition so that it talks to Obj-C. Then you can use Core Location to get the GPS position and feed that data back into the webview using a callback. It took some tinkering around but I eventually got it to work. – Andrew Sep 17 '13 at 15:07

1 Answers1

0

Just digging in some private headers I found this method:

  • (BOOL)webView:(WebView *)sender frame:(WebFrame *)frame requestGeolocationPermission:(WebGeolocation *)geolocation securityOrigin:(WebSecurityOrigin *)origin;

Did you try to see if it gets called? You probably won't get this past the App Store though, since it is a private API call.

Kristof Van Landschoot
  • 1,427
  • 1
  • 12
  • 30
  • This has to clear an App Store review, so no private methods. – Andrew Dec 14 '11 at 20:09
  • just tested: it does not get called; at least not from the WebUIDelegate. Maybe one has to register as WebUIPrivateDelegate... – auco Mar 08 '13 at 18:20