In my app, I have a WebView with JavaScript in it that occasionally needs to call externally defined Obj-C functions. According to documentation, this should be simple by conforming to the WebScripting protocol, but I just can't get it to work.

I set up the bridge object like this:

[[webView windowScriptObject] setValue:self forKey:@"bridge"];

And the class implements both

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return NO; }
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name { return NO; }
- (void) externalFunction{ /* external code  */};

Now, I load a HTML string like this into the WebView:

<input type="button" value="Extern" onclick="bridge.externalFunction()" />

and you would expect externalFunction to run when the user clicks the button. However, that's just not the case. externalFunction is never executed.

[[webView windowScriptObject] valueForKey:@"bridge"] is self when I check from Cocoa. If I check from JavaScript, though, window.bridge is undefined.

I'm thoroughly confused. What's more, if I build a new app from scratch for testing purposes, the above works just fine. There must be some factor in my application that keeps the bridge from working. But what could that be?

Any help much appreciated.

___
Marcel Hansemann

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to