开发者

Calling MacRuby via JavaScript with the windowScriptObject and WebKit

I have a new MacRuby application. I'm trying to call a MacRuby method from JavaScript loaded in a webView within the application.

The Calling Objective-C Methods From JavaScript tutorial shows how to add a key to the webScriptObject that's value is an Objective-C object. Thus you can call Obj-C methods from JavaScript.

Unfortunately this does not work with MacRuby classes/methods. Below is a shorten version of my WebView's loadDelegate:

class WebLoadDelegate

attr_accessor :objc_bridge, :mr_bridge

def webView(sender, windowScriptObjectAvailable:windowScriptObject)
    scriptObject = windowScriptObject

    self.mr_bridge = MacRubyBridge.new();
    self.objc_bridge = JavaScriptBridge.instance();

    scriptObject.setValue(self.objc_bridge, forKey:"ObjCInstance")
    scriptObject.setValue(self.mr_bridge, forKey:"MacRubyInstance")
end

end

When the webScriptObject is available i add two keys to it: ObjCInstance and MacRubyInstance.

Here's the implementation of the ObjC class:

#import "JavaScriptBridge.h"

static JavaScriptBridge *gInstance = NULL;

@implementation Ja开发者_开发技巧vaScriptBridge

+ (JavaScriptBridge *)instance {
    gInstance = [[self alloc] init];

    return gInstance;
}

+ (NSString *) webScriptNameForSelector:(SEL)sel
{       
    return @"nameAtIndex";
}

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
{
    if (aSelector == @selector(nameAtIndex:)) return NO;
    return YES;
}

- (NSString *)nameAtIndex:(int)index {
    return @"works";
}


@end

And here's what's supposed to be the same thing in Ruby:

class MacRubyBridge

    def nameAtIndex(i)
        return "fails"
    end

    def self.webScriptNameForSelector(sel)
        return "nameAtIndex";
    end


    def self.isSelectorExcludedFromWebScript(sel)
        if (sel == :nameAtIndex) 
            false
        else
            true
        end
    end

end

The only problem is the Objective-C implementation works fine. On the JS side you can call:

window.ObjCInstance.nameAtIndex_(1) 

Which returns the string "works".

But the MacRuby implementation fails. When you call:

window.MacRubyInstance.nameAtIndex_(1) 

You get:

Result of expression 'window.MacRubyInstance.nameAtIndex_' [undefined] is not a function

The webScriptNameForSelector and isSelectorExcludedFromWebScript methods never get called on the MacRuby implementation. I think that's the problem, but I don't know why they aren't getting called.

Any help would be greatly appreciated.


I showed how to do that in this tutorial/blog post.

My guess is that in your case, the following code is the problem:

    def self.isSelectorExcludedFromWebScript(sel)
     if (sel == :nameAtIndex) 
         false
     else
        true
     end
   end

The selector sent as an argument is more than likely not equal to the symbol your put. Try to return false for all and see if that works. Try:

def self.isSelectorExcludedFromWebScript(sel); false end

Also, hopefully my example will help you fix this issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜