1、URL拦截方式
js-》oc
WKWebView的- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;中处理拦截
UIWebView在- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType中处理拦截
oc-》js
UIWebView使用
- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;回调js方法
WKWebView使用
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;回调js方法
2、JavascriptCore
在- (void)webViewDidFinishLoad:(UIWebView *)webView;回调后,UIWebView 的KVC方式获得JSContext
然后进行绑定;
特点是:可以同步的方式回调JS
JSContext的SubscriptSupport下标赋值绑定block的方法有特性:1;返回值可以有,可以没有返回值;2;参数可获取,也可以不获取
typedef void(^blockkkk)(); // 明确不能有返回值
typedef id(^blockkk)(); // 参数任意的
typedef id(^blockk)(void); // 明确不能有参数
blockkkk a=^(){
};
blockkk b=^(){
return @"";
};
blockkk b2=^(NSString*str){
return @"";
};
blockk c=^(){
return @"aa";
};
- (void)ocCallJavascript {
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
NSDictionary*dict =@{@"type":@"GETVERSION",@"data":@{@"version":@"1.0",@"bundleName":@"1111"}, @"sign":@"签名"};
NSString *jsonValueStr= [dict jsonStringEncoded] ;
//jsonValueStr = [self replaceJsonString:jsonValueStr];
[context evaluateScript:[NSString stringWithFormat:@"jsInterface('%@')",jsonValueStr]];
}
//注册方法;js会调用
-(void)javascriptCallOc {
// 获得JSContext
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
// 注册一个appInterface方法 以便js可以访问到
context[@"appInterface"] =
// ^( ) { // 可以不带参数
^(NSDictionary* data ) {
// 是在子线程中执行--WebThread
NSArray *args = [JSContext currentArguments];
// 获得js传过来的参数----第一种方法
for (JSValue *jsVal in args) {
if ([jsVal isObject ]) {
NSLog(@"%@", jsVal.toDictionary);
[self ocCallJavascript];
}
}
// 获得js传过来的参数---第二种方法
NSLog(@"%@", data);
NSDictionary*dict =@{@"type":@"GETVERSION",@"data":@{@"version":@"1.0",@"bundleName":@"1111"}, @"sign":@"签名"};;
// 同步的方式回传给js
return dict; // 可以不返回
};
}
#pragma mark - Json转化
-(NSString *)replaceJsonString : (NSString *)messageJSON {
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2028" withString:@"\\u2028"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2029" withString:@"\\u2029"];
return messageJSON;
}
html文件
<html>
<header>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function secondClick() {
// OC中JSContext注册appInterface方法;
var a = appInterface({
type: 'GETVERSION',
data: {},
sign: ''
});
document.getElementById('label2').innerHTML = a;
}
function jsInterface(data) {
//showAlert(message);
document.getElementById('label').innerHTML = data;
}
function showAlert(content) {
setTimeout(function() {
alert(content);
}, 1);
}
</script>
</header>
<body>
<h2> 这里是第二种方式 </h2>
<h2> JavaScriptCore-UIWebView支持 </h2>
<br/>
<br/>
<button type="button" onclick="secondClick()">Click Me!</button>
<br/>
<br/>
<b id="label">需要改变的标签</b>
<br/>
<br/>
<b id="label2">需要改变的标签</b>
</body>
</html>
3、MessageHandlers
对象WKScriptMessage中可以获得js传过来的参数;
// 注册
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"appInterface"];
// 接口
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
runJavaScriptXXXXX代理
alert弹框、文字输入框、 确认弹框
4、WebViewJavascriptBridge (原理同URL拦截)
网友评论