2017-07-21 2 views
0

Ich habe ein Webview, in dem ich Online-Chat öffnen muss, wenn es geladen wird. Ich habe einen JavaScript-Code für den Online-Chat. Ich bin verwirrt, wie kann ich es benutzen. Ich habe versucht, Apple Website zu öffnen funktioniert gut, aber wie kann ich Javascript-Code in Objective c verwenden. Ich bin ziemlich neu in der Sprache und das ist das erste Mal, dass ich das benutze. Ich habe JavaScript-Code in einem Ordner hinzugefügt und es dem Ressourcenordner in meiner App hinzugefügt.Wie benutzt man JavaScript in Ziel C in iOS?

Antwort

0

Versuchen Sie dies.

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); 
}]; 
+0

, was darin ist wKWebView seine zeigt Fehler. @ovo –

+0

Hier ist [WKWebView] (https://developer.apple.com/documentation/webkit/wkwebview) doc. – ovo

0

Kasse the apple doc. Es wird dir mehr helfen.

Hier ist the best example Skript in WebView zu verwenden.

Das ist etwas, das Sie in Ihrem Code verwenden können:

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

Ich habe HTML-Datei und JS-Datei in meinem Ordner Projektressourcen. @Nirmalsinh –

+0

https://stackoverflow.com/questions/5733883/loading-javascript-into-a-uiwebview-from-resources – Nirmalsinh

+0

Überprüfen Sie die obige Antwort. Es wird dir helfen. – Nirmalsinh