Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call JavaScript Function in objective C

Tags:

I am new programmer to objective C. I added some html file to Objective C. In this html file contains simple javascript function. I need to call that Function through thw objective C code. I tried lot of time by using google. But I could not find real way to do it. Bellow Contains html File :

<!DOCTYPE html> <html>     <head>         <script>             function myFunction()             {                 alert("Hello World!");             }             </script>     </head>      <body>         <button onclick="myFunction()">Try it</button>     </body> </html> 

I called this function like this : Is it correct or wrong ?? If it wrong, how to do this. Please help.

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()"]; 
like image 659
Himesh Avatar asked Jan 15 '13 08:01

Himesh


People also ask

How to call JavaScript function in C?

Emscripten is an LLVM-to-JavaScript compiler. It takes LLVM bitcode - which can be generated from C/C++, using llvm-gcc (DragonEgg) or clang, or any other language that can be converted into LLVM - and compiles that into JavaScript, which can be run on the web (or anywhere else JavaScript can run).

How do I run JavaScript in WKWebView?

Inject JavaScript to WKWebView with WKUserScript viewDidLoad() let config = WKWebViewConfiguration() let js = getMyJavaScript() let script = WKUserScript(source: js, injectionTime: . atDocumentEnd, forMainFrameOnly: false) config. userContentController. addUserScript(script) config.


2 Answers

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()"]; 

If all this code called from single place, than it won't work, because page load is asynchronous and page with JavaScript code is not ready at that moment. Try to call this methods [webView stringByEvaluatingJavaScriptFromString:@"myFunction()"]; in UIWebViewDelegate callback method –webViewDidFinishLoad:

like image 198
Serhii Mamontov Avatar answered Sep 18 '22 19:09

Serhii Mamontov


<!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> 

Add a html file to project folder. Add your javascript file also to project folder.

After adding the html file and this native code to your 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"); } 

Call this in your didLoad method [self loadMyWebView];

Here you can add any javascript file to the project and include it to the html file. You can call javascript function inside the tag. (exactly as you used to call js functions. )

You can pass any parameter to javascript function and return anything to objective function.

Remember ::: After adding all js file don't forget to add them to Copy Bundle Resources

To avoid warning ::: remove them from Compile Sources

like image 21
umakanta Avatar answered Sep 16 '22 19:09

umakanta