我有一个 Cocoa 应用程序,它使用 WebView 来显示 HTML 界面。我如何从 HTML 界面中的 Javascript 函数调用 Objective-C 方法?

有帮助吗?

解决方案

这记录在 开发者.apple.com.

其他提示

如果你想在 iPhone 应用程序中执行此操作,则需要使用 UIWebViewDelegate 方法 shouldStartLoadWithRequest 来实现:

这个API http://code.google.com/p/jsbridge-to-cocoa/ 为你做。它非常轻。

Apple 的文档相当绿色,对我来说几乎无法使用,因此我做了一个从 javascript 调用 Objective C 方法的概念证明,在 Cocoa 中反之亦然,尽管后者要容易得多。

首先确保您的 webview 作为 setFrameLoadDelegate:

[testWinWebView setFrameLoadDelegate:self];

您需要告诉 webview 在加载后立即监视特定对象:

- (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowScriptObject forFrame:(WebFrame *)frame {
    //add the controller to the script environment
    //the "ObjCConnector" object will now be available to JavaScript
    [windowScriptObject setValue:self forKey:@"ObjCConnector"];
}

然后是通信的业务:

// a few methods to log activity
- (void)acceptJavaScriptFunctionOne:(NSString*) logText {
    NSLog(@"acceptJavaScriptFunctionOne: %@",logText);
}
- (void)acceptJavaScriptFunctionTwo:(NSString*) logText {
    NSLog(@"acceptJavaScriptFunctionTwo: %@",logText);
}

//this returns a nice name for the method in the JavaScript environment
+(NSString*)webScriptNameForSelector:(SEL)sel {
    NSLog(@"%@ received %@ with sel='%@'", self, NSStringFromSelector(_cmd), NSStringFromSelector(sel));
    if(sel == @selector(acceptJavaScriptFunctionOne:))
        return @"functionOne"; // this is what you're sending in from JS to map to above line
    if(sel == @selector(acceptJavaScriptFunctionTwo:))
        return @"functionTwo"; // this is what you're sending in from JS to map to above line
    return nil;
}

//this allows JavaScript to call the -logJavaScriptString: method
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel {
    NSLog(@"isSelectorExcludedFromWebScript: %@", NSStringFromSelector(sel));
    if(sel == @selector(acceptJavaScriptFunctionOne:) ||
       sel == @selector(acceptJavaScriptFunctionTwo:))
        return NO;
    return YES;
}

关键是,如果您想要调用多个方法,则需要将它们全部排除在 isSelectorExcludedFromWebScript 方法中,并且需要 javascript 调用来映射到 webScriptNameForSelector 中的 ObjC 方法。

完整的项目概念证明文件:https://github.com/bytesudios/JS-function-and-ObjC-method-connector

我有一个使用 NimbleKit 的解决方案。它可以从 Javascript 调用 Objective C 函数。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top