//webView加载html文件
NSURL *url = [[NSBundle mainBundle]URLForResource:@"oc和js" withExtension:@"html"];
self.webView.delegate = self;
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"====%@ ====%@===%@",[request URL],request,[[request URL] scheme]);
NSURL *url = [request URL];
NSString *urlStr = [url absoluteString];
if([urlStr containsString:@"file"]){
NSLog(@"包含");
}else{
NSLog(@"不包含");
}
//JS给OC传值方法1:裁剪参数
if([[url scheme] isEqualToString:@"firstclick12"]){
NSArray *params = [url.query componentsSeparatedByString:@"&"];
NSMutableArray *temArray = [NSMutableArray arrayWithCapacity:0];
for (NSString *parmStr in params) {
NSArray *contentArray = [parmStr componentsSeparatedByString:@"="];
if(contentArray.count>1){
//编码
NSString *decodeValue = @"";
if([[[UIDevice currentDevice]systemVersion]floatValue]<9.0){
decodeValue = [contentArray[1] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}else{
decodeValue = [contentArray[1] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
[temArray addObject:decodeValue ];
}
}
NSLog(@"tempDic:%@",temArray);
return NO;
}
return YES;
}
(void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"执行啦");
self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//用于捕获错误信息
self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
NSLog(@"异常信息:%@", exceptionValue);
};
NSLog(@"=====%@",webView.request.URL.absoluteString);
//OC给JS传值
[self call];
[self changeColor];
//JS给OC传值 -->此方法前端人员不一定实现
self.jsContext[@"loadURL"] = ^(){
NSArray *array = [JSContext currentArguments];
for (NSString *string in array) {
NSLog(@"string12======%@",string);
}
};
self.jsContext[@"Show"] = ^(){
NSArray *array = [JSContext currentArguments];
for (NSString *string in array) {
NSLog(@"string======%@",string);
}
};
self.jsContext[@"share"] = ^(){
NSArray *args = [JSContext currentArguments];
for (JSValue *jsVlu in args) {
NSLog(@"jsVlu.toString====%@",jsVlu.toString);
}
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"方式二" message:@"这是OC原生的弹出窗" delegate:self cancelButtonTitle:@"收到" otherButtonTitles:nil];
[alertView show];
});
};
//获取标签的内容
NSString *htmlNum = @"document.getElementById('isInApp').value";
NSString *numHtmlInfo = [webView stringByEvaluatingJavaScriptFromString:htmlNum];
NSLog(@"numHtmlInfo====%@",numHtmlInfo);
NSString *str = @"document.getElementById('isInApp').value='flase';";
[self.webView stringByEvaluatingJavaScriptFromString:str];
NSString *allHtml = @"document.documentElement.innerHTML";
NSString *allHtmlInfo = [webView stringByEvaluatingJavaScriptFromString:allHtml];
// NSLog(@"=====%@",allHtmlInfo);
NSString *numHtmlInfo1 = [webView stringByEvaluatingJavaScriptFromString:htmlNum];
// NSLog(@"numHtmlInfo====%@",numHtmlInfo1);
}
(void)call{
// 之后在回调js的方法Callback把内容传出去
JSValue *Callback = self.jsContext[@"Callback"];
//传值给web端
[Callback callWithArguments:@[@"江弘",@"15757166458"]];
}
(void)changeColor{
// 之后在回调js的方法Callback把内容传出去
JSValue *Callback = self.jsContext[@"changeColor"];
//传值给web端
[Callback callWithArguments:@[@"red"]];
}