如何在目标C中调用JavaScript函数
我是新的程序员目标C.我添加了一些HTML文件到目标C.在这个HTML文件包含简单的JavaScript函数。 我需要通过客观的C代码来调用这个函数。 我通过使用谷歌了很多时间。 但我找不到真正的方法来做到这一点。 贝娄包含html文件:
<!DOCTYPE html> <html> <head> <script> function myFunction() { alert("Hello World!"); } </script> </head> <body> <button onclick="myFunction()">Try it</button> </body> </html>
我这样调用这个函数:是正确还是错误? 如果错了,该怎么做。 请帮忙。
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()"];
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()"];
如果所有这些代码都是从单独的地方调用的,那么它将不起作用,因为页面加载是asynchronous的,而JavaScript代码的页面在那一刻还没有准备好。 尝试调用这个方法[webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
在UIWebViewDelegatecallback方法–webViewDidFinishLoad:
<!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>
添加一个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"); }
在你的didLoad方法[self loadMyWebView];
调用它[self loadMyWebView];
在这里,您可以添加任何JavaScript文件到项目,并将其包括到HTML文件。 你可以在标签里面调用javascript函数。 (就像你用来调用js函数一样)。
您可以将任何parameter passing给javascript函数,并返回任何目标函数。
记住:::添加所有js文件后,不要忘记将它们添加到复制包资源
为了避免警告:::从编译源中删除它们
可以使用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
这里是一个演示:
FileName.js:
function greet() { hello(“This is Javascript function!”); }
将该文件复制到Bundle资源中。
现在在头文件中input#import <JavaScriptCore/JavaScriptCore.h>
@property (nonatomic,strong) JSContext *context; // Declare this in header file // This code block goes into main file NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"js"]; NSString *scriptString = [NSString stringWithContentsOfFile:scriptPath encoding:NSUTF8StringEncoding error:nil]; self.context = [[JSContext alloc] init]; [self.context evaluateScript:scriptString]; self.context[@"hello"] = ^(NSString text) { NSLog(@"JS : %@",text); } JSValue *function = self.context[@"greet"]; [function callWithArguments:@[]];
这个代码块为我工作。 它显示“这是Javascriptfunction!” 在控制台。 希望这个作品!
尝试这个:
NSString *jsCallBack = [NSString stringWithFormat:@"setPhoto('%@')",profileImgPath]; [webView stringByEvaluatingJavaScriptFromString:jsCallBack];