0

I have a webview in which i have to open online chat when it is loads. I have a javascript code for the online chat . I'm confused that how can i use it. I have tried to open apple website its working fine , but how can i use javascript code in Objective c. I'm quite new to language and this is first time i'm using this. I have added javascript code in a folder and add it to the resources folder in my app.

Hamza Imran
  • 189
  • 1
  • 18

2 Answers2

0

Try this.

NSString *scriptURL = [[NSBundle mainBundle] pathForResource:@"JavaScript" ofType:@"js"];
NSString *scriptContent = [NSString stringWithContentsOfFile:scriptURL encoding:NSUTF8StringEncoding error:nil];

[wkWebView evaluateJavaScript: scriptContent completionHandler:^(id _Nullable content, NSError * _Nullable error)
 {
     if (error) {
         NSLog(@"Error: %@",error);
         return;
     }

     NSLog(@"Content: %@",content);
 }];
ovo
  • 1,904
  • 13
  • 26
  • Here is [WKWebView](https://developer.apple.com/documentation/webkit/wkwebview) doc. – ovo Jul 24 '17 at 01:26
0

Checkout the apple doc. It will help you more.

Here is the best example to use script in webView.

This is something that you can use into your code:

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()"];
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56