https://www.cnblogs.com/huaixu/p/5570100.html
https://www.cnblogs.com/qiyiyifan/p/12077700.html
1、webview去除原网址的广告或者标题
js语句
document.documentElement.getElementsByClassName(‘这里写你要消除的空间的class里面的字符串‘)[0].style.display = ‘none‘
具体用法:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName(‘adpic‘)[0].style.display = ‘none‘"];
}
2. 获得UIWebView的标题
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
3. 获取页面的URL
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
}
4. 修改页面元素的值
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName(‘q‘)[0].value=‘朱祁林‘;"];
}
5. 表单提交
- (void)webViewDidFinishLoad:(UIWebView *)webView{
一、前言
这个星期面临一个网页的需求,需要用到这方面的知识,很久没看过这方面的代码了,记录下吧。
wkwebview的创建和使用就不再提及了,直接看与JS之间的交互吧。
二、内容
1、JS调用OC
window.webkit.messageHandlers.<#对象名#>.postMessage(<#参数#>)
//进行配置控制器 WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; //实例化对象 configuration.userContentController = [WKUserContentController new]; //调用JS方法 [configuration.userContentController addScriptMessageHandler:self name:@"actionEnd"];
#pragma mark - WKScriptMessageHandler - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { if ([message.name isEqualToString:@"actionEnd"]) { NSDictionary *jsData = message.body; NSLog(@"%@", message.name, jsData); } }
注:对象名要和前端沟通好。前端JS能回传的类型只能是 NSNumber, NSString, NSDate, NSArray, NSDictionary, NSNull。
2、OC调用JS
// 调用API方法 [self.weexWebView evaluateJavaScript:@“JS” completionHandler:^(id object, NSError * _Nullable error) { NSLog(@"obj:%@---error:%@", object, error); }];