webScriptNameForSelector和带下划线的javascript方法 [英] webScriptNameForSelector and javascript method with underscore
问题描述
我在应用程序中使用了WebView,正在加载的页面上有一些在Windows环境中使用过的现有javascript函数调用,现在应该在mac应用程序中使用.我的问题是当前的javascript(从可运行以下功能的Windows应用程序移植)函数像这样调用目标C:
I am using a WebView in my application and it the page I am loading has some existing javascript function calls that were used in a windows environment and should now be used in a mac application. My issue is that the current javascript (ported from a windows app where the following works) function calls objective C like this:
function callObjectiveC
{
window.external.MY_ObjectiveCFunction();
}
我一直在尝试使用:
+ (NSString *)webScriptNameForSelector:(SEL)sel
{
if (sel == @selector(MyObjectiveCFunction))
{
return @"My_ObjectiveCFunction";
}
}
基于: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/WebKit/Protocols/WebScripting_Protocol/Reference/Reference.html ,但无济于事.有没有办法做到这一点?我可以随意调用目标C方法,只需将其映射到现有javascript中即可.
based on: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/WebKit/Protocols/WebScripting_Protocol/Reference/Reference.html but to no avail. Is there a way to do this? I can call the objective C method whatever I want I just need to be able to map that to what is in the existing javascript.
谢谢, c.
推荐答案
想出了一个解决方案:
- (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
{
NSLog(@"invoke undefined method: %@", name);
if([name isEqualToString:@"My_ObjectiveCFunction"])
{
return NSSelectorFromString(@"My_ObjectiveCFuntion");
}
return nil;
}
这篇关于webScriptNameForSelector和带下划线的javascript方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!