WKWebView在執(zhí)行內(nèi)存上圓圓高于UIWebView
不多說(shuō)直接上代碼
不管是移除網(wǎng)頁(yè)內(nèi)容,還是給圖片慈鸠,按鈕添加點(diǎn)擊青团,都是一個(gè)套套路芦昔,找節(jié)點(diǎn)烟零,如果不會(huì)找節(jié)點(diǎn)锨阿,Web開發(fā)會(huì)
#import "ViewController.h"
#import <WebKit/WebKit.h>
@interface ViewController ()<WKNavigationDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
WKWebView *webView = [[WKWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
webView.navigationDelegate = self;
[self.view addSubview:webView];
NSURL *url = [NSURL URLWithString:@"http://m.dianping.com/tuan/deal/5501525"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
/*
*
* 頁(yè)面加載完成時(shí)候調(diào)用 JS注入
*/
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
NSMutableString *MString = [NSMutableString string];
// 刪除網(wǎng)頁(yè)內(nèi)不需要的內(nèi)容
//1. 頂部的返回 --> 起名字 結(jié)尾要加;
[MString appendFormat:@"var headerTag = document.getElementsByTagName('header')[0];"];
//刪除時(shí)需要找到父標(biāo)簽 --> 自己干掉自己, 下不去手
//parentNode:父標(biāo)簽
//removeChild:移除子標(biāo)簽內(nèi)容
[MString appendFormat:@"header.parentNode.removeChild(header);"];
[MString appendFormat:@"var buy = document.getElementsByClassName('buy-now ')[0];"];
[MString appendFormat:@"buy.parentNode.removeChild(buy);"];
//給image添加點(diǎn)擊事件
[MString appendString:@"var imgTag = document.getElenmentsByTagName('figure')[0].children[0];imgTag.onclick=function imgClick(){window.location.];
[webView evaluateJavaScript:MString completionHandler:nil];
}
/*
*
* 網(wǎng)頁(yè)即將開始時(shí)候調(diào)用桐智,攔截標(biāo)簽點(diǎn)擊發(fā)送的請(qǐng)求
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString *urlString = navigationAction.request.URL.absoluteString;
NSLog(@"%@",urlString);
if ([urlString isEqualToString:@"https://www.baidu.com"]) {
//fuck come fuck go
}
decisionHandler(WKNavigationActionPolicyAllow);
}
@end
!!!!!!!以上是oc調(diào)用js.下面來(lái)js調(diào)用oc!!!!!!
#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <XLPhotoBrowser+CoderXL/XLPhotoBrowser.h>
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<WKUIDelegate,WKScriptMessageHandler>
@property (strong, nonatomic) WKWebView *webView;
@property (strong, nonatomic) UIProgressView *progressView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"圖片下載測(cè)試";
//初始話WKWebView
[self initWKWebView];
//初始化進(jìn)度條
[self initProgressView];
//
//添加進(jìn)度條監(jiān)聽
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)initWKWebView{
//進(jìn)行配置控制器
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
//實(shí)例化對(duì)象
configuration.userContentController = [WKUserContentController new];
[configuration.userContentController addScriptMessageHandler:self name:@"imgClick"];
WKPreferences *preferences = [WKPreferences new];
preferences.javaScriptCanOpenWindowsAutomatically = YES;
preferences.minimumFontSize = 40.0;
configuration.preferences = preferences;
self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"saner.html" ofType:nil];
NSURL *fileURL = [NSURL fileURLWithPath:urlStr];
[self.webView loadFileURL:fileURL allowingReadAccessToURL:fileURL];
self.webView.UIDelegate = self;
[self.view addSubview:self.webView];
}
- (void)initProgressView{
CGFloat kScreenWidth = [[UIScreen mainScreen] bounds].size.width;
UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 2)];
progressView.tintColor = [UIColor redColor];
progressView.trackTintColor = [UIColor lightGrayColor];
[self.view addSubview:progressView];
self.progressView = progressView;
}
#pragma mark - KVO
// 計(jì)算wkWebView進(jìn)度條
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"]) {
CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
if (newprogress == 1) {
[self.progressView setProgress:1.0 animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.progressView.hidden = YES;
[self.progressView setProgress:0 animated:NO];
});
}else {
self.progressView.hidden = NO;
[self.progressView setProgress:newprogress animated:YES];
}
}
}
//當(dāng)把JS返回給控制器,然后彈窗就是這樣設(shè)計(jì)的
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
NSLog(@"%@",message);
completionHandler();
}
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
// message.body -- Allowed types are NSNumber, NSString, NSDate, NSArray,NSDictionary, and NSNull.
if ([message.name isEqualToString:@"imgClick"]) {
[self downImage:message.body[@"url"]];
}
}
-(void)downImage:(NSString *)url{
NSArray *images = @[url];
[XLPhotoBrowser showPhotoBrowserWithImages:images currentImageIndex:0];
}
html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<script language="javascript">
function imgClick() {
window.webkit.messageHandlers.imgClick.postMessage({title:'張健標(biāo)題',content:'張健內(nèi)容',url:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1520591158463&di=31dc24992c2a5f6385c69e0b901604bd&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F55e736d12f2eb938e41eb7d7d9628535e5dd6fa0.jpg'});
}
</script>
</head>
<body>
<input type="button" value="掃一掃" onclick="imgClick()" />
</body>
</html>