WebKit: Javascript - Cocoa bridge not working
WebKit: Javascript - Cocoa bridge not working
- Subject: WebKit: Javascript - Cocoa bridge not working
- From: Marcel Hansemann <email@hidden>
- Date: Fri, 3 Apr 2009 23:42:32 +0200
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 (email@hidden)
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:
This email sent to email@hidden