views:

427

answers:

1

Hi, all.

I'm working on a new feature for my Mac application and it has to do with maps. As an example, let's use iPhoto's "Places".

iPhoto uses Google Maps to show the location of various photos with red pins. Now when I click onto one of those pins, the application itself - iPhoto - reacts to that click, not the WebView / the map. So when I click onto a pin, the view changes and it shows all photos in that location. Not on a map, but in a different screen within iPhoto. So I assume iPhoto gets some sort of callback from the JavaScript / Map so that it can change the view.

This is what I am trying to achieve. Getting a callback from Google Maps back into my Objective-C / Cocoa code so I can change something inside my application, not the Map itself, depending on the marker the user has clicked on.

Thank you for any help, Kind regards,

Matthias

+2  A: 

With UIWebView, I don't think there is a direct way to get a callback. One crude way would be to have a NSTimer periodically check the state/value of a javascript variable in the page by calling stringByEvaluatingJavaScriptFromString. Based on the value, the objective c code can do something. However, I don't recommend this.

It might be better to use an MKMapView instead which will give you easier, more direct access to the map (although MKMapView doesn't have all the features of the full Google Maps). I haven't tested this but this article shows a way to detect when a MKAnnotation pin is selected.

DyingCactus
oh, darn, I should have mentioned this is for Mac. But thanks for the link, I'll check it out. MKMapView is not available on the Mac as far as I know...
Matthias Gansrigler
On the mac, this appears to be possible using WebKit. See http://developer.apple.com/mac/library/DOCUMENTATION/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html#//apple_ref/doc/uid/30001215-BBCBFJCD
DyingCactus
It says that I can _not_ access Objective-C properties et al. through JavaScript with this... Weird. how does apple do it?
Matthias Gansrigler
I think what they mean is that only javascript on a page your app is hosting in WebKit can do it. It won't allow some javascript running in Safari say to call objC.
DyingCactus
Oh, man, sorry. I misinterpreted the first sentence of that document you linked. I get how it's done. Thank you for your time and patience!
Matthias Gansrigler
You're welcome. Hope it works out.
DyingCactus