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.
Asked
Active
Viewed 88 times
2 Answers
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
-
I have html file and js file in my project resources folder. @Nirmalsinh – Hamza Imran Jul 21 '17 at 10:08
-
https://stackoverflow.com/questions/5733883/loading-javascript-into-a-uiwebview-from-resources – Nirmalsinh Rathod Jul 21 '17 at 10:11
-