Re: Web scripting key does not work

Christiaan Hofman <[email protected]> Wed, 20 Oct 2010 17:29:11 +0200
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
On Oct 20, 2010, at 17:13, aglee wrote:

> I looked at the CallJS example code and it shows how you can make an *instance variable* of your Objective-C object "shared" so that JavaScript can access it.  But in that example, the instance variable is not declared as a @property, so there is no getter method whose name could conflict with the name of the instance variable.  I think this merits at least a documentation bug, since the docs clearly state that you can access properties but give no examples of how to do so:
> 
> <http://developer.apple.com/library/mac/#documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html>
> 
> The other code examples might be more helpful, and I have a vague feeling I read something on this topic once -- I might poke around more.
> 
> --Andy
> 


I would expect it's just KVC, and that's also mentioned in the docs IIRC. The CallJS sample also implements KVC compliant accessors for the key it exposes. AFAICS I am doing exactly the same as that sample to expose my key, but it does nothing.

Christiaan

> 
> On Oct 20, 2010, at 10:53 AM, aglee <[email protected]> wrote:
> 
>> I tried the same thing in our WebView and got
>> 
>> [<MyClass 0x4cea90> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key foo
>> 
>> I suspect you can't use dot notation in JavaScript to access Objective-C properties, because window.myObject.foo would be ambiguous -- is its value the property value, or is it the *function* that retrieves the property value?  To illustrate, if in the JavaScript I do alert(window.myObject.foo), the alert message is:
>> 
>> function foo() {
>>     [native code]
>> }
>> 
>> I played with this a while back but I don't remember what the docs said.
>> 
>> --Andy
>> 
>> 
>> On Oct 20, 2010, at 09:18 AM, Christiaan Hofman <[email protected]> wrote:
>> 
>> I am trying to make a key for an object available to javascript in a webview. I have been able to successfully do this for calling methods on the object, but exposing the key does not work. I have followed all the prescriptions in the documentation:
>> 
>> - Implemented -webView:didClearWindowObject:forFrame: in the WebFrameLoadDelegate
>> 
>> - (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowObject forFrame:(WebFrame *)frame {
>> [windowObject setValue:[FOO sharedInstance] forKey:@"foo"];
>> }
>> 
>> - Implemented +isKeyExcludedFromWebScript: on the class (Foo) for the exposed object, returning NO for the (KVC compliant) key I want to expose.
>> 
>> + (BOOL)isKeyExcludedFromWebScript:(const char *)name {
>> return strcmp(name, "bar") != 0;
>> }
>> 
>> Now when I try to set the key of the object in a JavaScript function on a page loaded in my webview (window.foo.bar = true), the KVC compliant setter is never called, and nothing happens. As I said, calling a function for an exposed selector from JavaScript does work, the method is properly called.
>> 
>> Is this a bug, or has webkit stopped supporting this, or is there something else I forgot?
>> 
>> thanks,
>> Christiaan
>> 
>> _______________________________________________
>> MacOSX-dev mailing list
>> [email protected]
>> http://www.omnigroup.com/mailman/listinfo/macosx-dev
>> _______________________________________________
>> MacOSX-dev mailing list
>> [email protected]
>> http://www.omnigroup.com/mailman/listinfo/macosx-dev