如何在目标C中调用JavaScript函数

Him*_*esh 24 uiwebview ios

我是目标C的新程序员.我在Objective C中添加了一些html文件.在这个html文件中包含简单的javascript函数.我需要通过目标C代码调用该函数.我通过谷歌尝试了很多时间.但我找不到真正的方法来做到这一点.Bellow包含html文件:

<!DOCTYPE html>
<html>
    <head>
        <script>
            function myFunction()
            {
                alert("Hello World!");
            }
            </script>
    </head>

    <body>
        <button onclick="myFunction()">Try it</button>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我这样调用这个函数:是正确还是错误?如果错了,该怎么做.请帮忙.

NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:@"first" ofType:@"html"];
NSURL *instructionsURL = [NSURL fileURLWithPath:path];
[webView loadRequest:[NSURLRequest requestWithURL:instructionsURL]];

 NSString * jsCallBack = [NSString stringWithFormat:@"myFunction()"];
 [webView stringByEvaluatingJavaScriptFromString:jsCallBack];

[webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
Run Code Online (Sandbox Code Playgroud)

Ser*_*tov 30

NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:@"first" ofType:@"html"];
NSURL *instructionsURL = [NSURL fileURLWithPath:path];
[webView loadRequest:[NSURLRequest requestWithURL:instructionsURL]];

 NSString * jsCallBack = [NSString stringWithFormat:@"myFunction()"];
 [webView stringByEvaluatingJavaScriptFromString:jsCallBack];

[webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
Run Code Online (Sandbox Code Playgroud)

如果从单个位置调用所有这些代码,那么它将无法工作,因为页面加载是异步的,并且当时没有准备好带有JavaScript代码的页面.尝试[webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];在UIWebViewDelegate回调方法中调用此方法–webViewDidFinishLoad:


uma*_*nta 11

<!DOCTYPE html>
<html>
<head>

    <script type="text/javascript" charset="utf-8" src="your.js"></script>
    <script type="text/javascript" charset="utf-8" src="andYourJsfileName.js"></script>

    <script>
        function myFunction('yourParameter') 
        {
            // do your javascript operation

            //alert("my Hello World!");
            return value;
        }

    </script>
</head>

<body>
    <!--button onclick="myFunction()">Try it</button-->
</body>
Run Code Online (Sandbox Code Playgroud)

将html文件添加到项目文件夹.将您的javascript文件也添加到项目文件夹中.

将html文件和此本机代码添加到viewController.m后

-(void)loadMyWebView {
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
    NSString *path;
    NSBundle *thisBundle = [NSBundle mainBundle];
    path = [thisBundle pathForResource:@"first" ofType:@"html"];
    NSURL *instructionsURL = [NSURL fileURLWithPath:path];

    NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    webView.delegate=self;

    [webView loadHTMLString:htmlString baseURL:instructionsURL];
    // [self.view addSubview:webView];  (if you want to access the javascript function and don't want to add webview, you can skip that also.)
}


- (void)webViewDidFinishLoad:(UIWebView*)theWebView {
    NSString* returnValue =[theWebView stringByEvaluatingJavaScriptFromString:@"myFunction('pass your parameter')"];
    NSLog(@"returnValue = %@ ",returnValue);
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    NSLog(@" didFailLoadWithError");
}
Run Code Online (Sandbox Code Playgroud)

在didLoad方法中调用它 [self loadMyWebView];

在这里,您可以将任何javascript文件添加到项目中,并将其包含在html文件中.你可以在标签内调用javascript函数.(就像你以前调用js函数一样.)

您可以将任何参数传递给javascript函数并将任何内容返回给目标函数.

记住::: 添加所有js文件后不要忘记将它们添加到Copy Bundle Resources

为了避免警告:::Compile Sources中删除它们


Evg*_*nii 8

可以使用JavaScriptCore框架从Objective-C运行JavaScript代码.

#import <JavaScriptCore/JavaScriptCore.h>

...

JSContext *context = [[JSContext alloc] init];
[context evaluateScript: @"function greet(name){ return 'Hello, ' + name; }"];
JSValue *function = context[@"greet"];
JSValue* result = [function callWithArguments:@[@"World"]];
[result toString]; // -> Hello, World
Run Code Online (Sandbox Code Playgroud)

这是一个演示:

https://github.com/evgenyneu/ios-javascriptcore-demo