WKWebView的實(shí)現(xiàn)
需求: 我們經(jīng)常在項(xiàng)目中加載網(wǎng)頁(yè),但是點(diǎn)擊網(wǎng)頁(yè)的事件需要調(diào)用本地OC代碼
1悍引、配置WKWebView
- (void)initWKWebView
{
? ? NSString *hidenNavAndTabScript = @"$(document).ready(function(){$(\"#canvasBg\").css('top','0');$(\"#header\").hide();$(\"#header\").next().hide();$(\"#footer\").hide();});$(\"#gund\").css('top','0');$(\".diymenu\").hide();$(\".fui-content\").css('bottom','0').css('padding-bottom', '0')";
? ? //WKUserScriptInjectionTimeAtDocumentEnd為網(wǎng)頁(yè)加載完成時(shí)注入
? ? WKUserScript *script = [[WKUserScript alloc] initWithSource:hidenNavAndTabScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
? ? //根據(jù)生成的WKUserScript對(duì)象运准,初始化WKWebViewConfiguration
? ? WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
?? ? ? ? WKPreferences*preferences = [WKPreferencesnew];
?? ? ? ? preferences.javaScriptCanOpenWindowsAutomatically = YES;
?? ? ? ? configuration.preferences= preferences;
? ? [configuration.userContentControlleraddUserScript:script];
? ? self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight-kNavHeight) configuration:configuration];
? ? NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.infoUrl]];
? ? [requestsetValue:@"www.suxiangshidai.com://" forHTTPHeaderField:@"Referer"];
? ? [self.webViewloadRequest:request];
? ? self.webView.UIDelegate = self;
? ? self.webView.navigationDelegate = self;
? ? [self.viewaddSubview:self.webView];
}
2掏颊、JS調(diào)用OC
2.1 某抓、 //意思是網(wǎng)頁(yè)中需要傳遞的參數(shù)是通過這個(gè)JS中的showMessage方法來傳遞的
- (void)viewWillAppear:(BOOL)animated
{
? ? [superviewWillAppear:animated];
? ? [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"ScanAction"];
? ? [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"CustomerServiceChatAction"];
? ? [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"saveImageToAlbum"];
? ? [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"saveMultImages"];
}
- (void)viewWillDisappear:(BOOL)animated
{
? ? [super viewWillDisappear:animated];
? ? // 因此這里要記得移除handlers
? ? [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"ScanAction"];
? ? [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"CustomerServiceChatAction"];
? ? [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"saveImageToAlbum"];
? ? [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"saveMultImages"];
}
2.2揖膜、代理二:接收J(rèn)S端發(fā)過來的消息看锉,并處理相應(yīng)的業(yè)務(wù)邏輯姿锭,
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message
{
? ? NSLog(@"body:%@",message.body);
? ? if([message.nameisEqualToString:@"ScanAction"]) {
? ? ? ? [selfscanEvent];
? ? }else if ([message.name isEqualToString:@"CustomerServiceChatAction"]){
? ? ? ? [self CustomerServiceChatAction:message.body];
? ? }elseif([message.nameisEqualToString:@"saveImageToAlbum"]){
? ? ? ? [self saveImageToAlbum];
? ? ? ? NSLog(@"saveImageToAlbum");
? ? }elseif([message.nameisEqualToString:@"saveMultImages"]){
? ? ? ? [selfsaveMultImages:message.body];
? ? }
}
3塔鳍、OC調(diào)用JS
3.1、動(dòng)態(tài)注入js方法
- (void)evaluateJavaScript:(NSString*)javaScriptString completionHandler:(void(^ _Nullable)(_Nullableid,NSError* _Nullable error))completionHandler;
// 此處是設(shè)置需要調(diào)用的js方法以及將對(duì)應(yīng)的參數(shù)傳入呻此,需要以字符串的形式NSString*jsFounction = [NSStringstringWithFormat:@"getAppConfig('%@')", APP_CHANNEL_ID];//?
調(diào)用API方法
[self.weexWebView evaluateJavaScript:jsFounction completionHandler:^(idobject,NSError* _Nullable error) {
NSLog(@"obj:%@---error:%@", object, error);?
?}];