2017-07-21 8 views
0

私はロード時にオンラインチャットを開く必要があるwebviewを持っています。私はオンラインチャット用のjavascriptコードを持っています。私はそれをどのように使うことができるのか混乱しています。アップルのウェブサイトを開こうとしましたが、Objective cでjavascriptコードを使用するにはどうすればいいですか?私は言語にはまったく新しいです。これは初めてこれを使用しています。私はフォルダにjavascriptコードを追加し、私のアプリのリソースフォルダに追加しました。iOSの目的Cでjavascriptコードを使用するには?

答えて

0

これを試してください。

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

これは、あなたのコードに使用することができるものです。 @ovo –

+0

[WKWebView](https://developer.apple.com/documentation/webkit/wkwebview)docです。 – ovo

0

チェックアウトthe apple doc。それはあなたにもっと役立つでしょう。

webViewでスクリプトを使用する場合はthe best exampleです。その中wKWebViewその示すエラーが何であるかを

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

プロジェクトリソースフォルダにhtmlファイルとjsファイルがあります。 @Nirmalsinh –

+0

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

+0

上記の回答を確認してください。それはあなたを助けるでしょう。 – Nirmalsinh

関連する問題