通过 WebScriptObject 将 Cocoa 对象暴露给 JS 脚本环境

发布于 2024-11-06 19:48:42 字数 1226 浏览 0 评论 0原文

我看到有几个类似的问题,但这是一个更基本的问题。

我试图通过 WebScriptObject 向 WebView 公开一个简单的 Cocoa 对象,希望允许页面向 Cocoa 对象发送消息。关于此的文档非常清楚,但由于某种原因我无法让它工作。想知道您是否愿意看一下...

这是我正在加载到脚本环境中的对象。

@interface Client : NSObject {

     NSString *test;

}

@implementation Client

- (id)init {

    self = [super init];
    test = [[NSString alloc] initWithString:@"Hey Simon"];
    return self;

}

- (NSString *)test {

    return test;
}

然后我在 WebView 的 FrameLoad 委托中加载该对象,其中:

- (void)webView:(WebView *)webView didClearWindowObject:(WebScriptObject *)windowObject forFrame:(WebFrame *)frame {

    Client *_client = [[Client alloc] init];
    [windowObject setValue:_client forKey:@"client"];

}

在 JS 方面,我只是做了一些非常基本的事情:

if( 'client' in window ) {

var client = window.client;

console.log( '---' );

console.log( 'client.test(): ' + client.test() );

console.log( '---' );

}

JS 控制台显示 TypeError: 表达式 'client.test' [undefined] 的结果不是 a功能。

有几件事。我知道该对象正在正确加载到脚本环境中,因为它不会通过条件加上我可以看到以下描述:

console.log( 'Client object: ' + client )

但我只是不知道如何正确公开我的可可方法。看看我上面所说的,我在类中实现方法或在 JS 中调用它们的方式是否存在问题?

提前致谢, 亚历克

I see there are a couple of similar questions, but this is a bit more basic.

I am trying to expose a simple Cocoa object via WebScriptObject to a WebView, hopefully allowing the page to send messages to the Cocoa object. The documentation on this is very clear, but for some reason I can't get it to work. Wondering if you'll have a look...

Here is the Object that I'm loading into the scripting environment.

@interface Client : NSObject {

     NSString *test;

}

@implementation Client

- (id)init {

    self = [super init];
    test = [[NSString alloc] initWithString:@"Hey Simon"];
    return self;

}

- (NSString *)test {

    return test;
}

Then I'm loading that object in the WebView's frameLoad delegate, with:

- (void)webView:(WebView *)webView didClearWindowObject:(WebScriptObject *)windowObject forFrame:(WebFrame *)frame {

    Client *_client = [[Client alloc] init];
    [windowObject setValue:_client forKey:@"client"];

}

On the JS side of things, I am just doing something really basic:

if( 'client' in window ) {

var client = window.client;

console.log( '---' );

console.log( 'client.test(): ' + client.test() );

console.log( '---' );

}

The JS console says TypeError: Result of expression 'client.test' [undefined] is not a function.

A couple of things. I know the object is being loaded into the scripting environment properly because it wouldn't pass the conditional plus I can see a description with:

console.log( 'Client object: ' + client )

But I just don't know how to expose my cocoa methods properly. Looking at what I've said above, is there some problem with how I am implementing the methods in my class, or calling them in JS?

Thanks in advance,
Alec

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2024-11-13 19:48:42

它就在文档中。 :(

您必须在传入的对象上实现 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector 。

所以就我而言,我必须编写

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector

{
    if (aSelector == @selector(test)) return NO;
    return YES;
}

It was right there in the docs. :(

You have to implement + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector on the object you are passing in.

So in my case, I had to write

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector

{
    if (aSelector == @selector(test)) return NO;
    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文