在OC與JS交互時(shí)要導(dǎo)入#import<JavaScriptCore/JavaScriptCore.h>
1.創(chuàng)建一個(gè)webview,加載目標(biāo)html文件。
2.要清楚html文件的內(nèi)容,了解js的函數(shù)。
3.iOS調(diào)用html中的方法,使用系統(tǒng)自帶的具體實(shí)現(xiàn)如下:
//oc調(diào)用html的方法? =====前提是html方法中存在
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"showTitleMessage('%@')",@"oc調(diào)用了js的內(nèi)容"]];
//showTitleMessage 為html中的函數(shù)
4.js調(diào)用OC方法,首先在iOS端實(shí)現(xiàn)js中已經(jīng)定義但未實(shí)現(xiàn)的函數(shù),具體如下:
//獲取上下文文件(相當(dāng)一個(gè)操作的方法工具,用于獲取html中的方法和其它)
JSContext *content = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
/**
*? 為html中的方法賦值,定義html中bdgt的方法內(nèi)容
*任意html函數(shù)中的bdgt(參數(shù));格式的變量,都可以通過上下文獲取到并進(jìn)行復(fù)制操作,調(diào)用oc的方法。
即content[@"begt"]=^(){}
*
*/
content[@"bdgt"] = ^() {
NSLog(@"js調(diào)用oc---------begin--------");
//獲取html中bdgt的參數(shù)返回一個(gè)數(shù)組
NSArray *thisArr = [JSContext currentArguments];
for (JSValue *jsValue in thisArr) {
NSLog(@"=======%@",jsValue);
}
//獲取調(diào)用html方法的對象
JSValue *this = [JSContext currentThis];
NSLog(@"this: %@",this);
NSLog(@"js調(diào)用oc---------The End-------");
};
html代碼如下:

OC實(shí)現(xiàn)代碼如下:
