//1、该对象提供了通过js向web view发送消息的途径
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
//添加在js中操作的对象名称,通过该对象来向web view发送消息
// [userContentController addScriptMessageHandler:self name:@"FirstJsObect"];
[userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"acceptanceGet"];//vipProfit
[userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"tradeBuyGet"];//recommendAwards
[userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"helpCenterGet"];//recommendAwards
//2、
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
config.userContentController = userContentController;
//3、通过初试化方法,生成webview对象并完成配置
self.myCustomWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) configuration:config];
[self.myCustomWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
self.myCustomWebView.scrollView.scrollEnabled =YES;
self.myCustomWebView.navigationDelegate = self;
self.myCustomWebView.scrollView.delegate =self;
self.myCustomWebView.allowsBackForwardNavigationGestures = YES;
//实现协议方法。在这个方法里message参数有一个属性body。message.body就是JS传过来的参数,可以是字符串,可以是数组,也可以是字典。通过message.name判断可以知道监听的是JS的哪个方法。
#pragma mark - WKScriptMessageHandler
-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
NSLog(@"方法名:%@",message.name);
NSLog(@"内容:%@",message.body);
if ([message.name isEqualToString:@"acceptanceGet"]) {
//去找承兑商充值
UITabBarController *tabbar = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers[0];
[self.navigationController popToRootViewControllerAnimated:NO];
// self.navigationController.tabBarController.selectedIndex=2;
tabbar.selectedIndex = 2;
[self.navigationController dismissViewControllerAnimated:NO completion:nil];
[[NSNotificationCenter defaultCenter]postNotificationName:@"dismissAll" object:@"acceptanceGet"];
} else if ([message.name isEqualToString:@"tradeBuyGet"]) {
//tradeBuyGet通过交易买入
UITabBarController *tabbar = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers[0];
[self.navigationController popToRootViewControllerAnimated:NO];
tabbar.selectedIndex = 1;
[self.navigationController dismissViewControllerAnimated:NO completion:nil];
[[NSNotificationCenter defaultCenter]postNotificationName:@"dismissAll" object:@"tradeBuyGet"];
}else if ([message.name isEqualToString:@"helpCenterGet"]) {
//helpCenterGet帮助中心
LCSystemBulletinViewController *lc=[LCSystemBulletinViewController new];
lc.titleStr=WDLocalizedString(@"帮助中心");
lc.urlStr=@"help_url";
[self.navigationController pushViewController: lc animated:YES];
}
}
- (void)dealloc{
WKUserContentController*userCC= self.myCustomWebView.configuration.userContentController;
[userCC removeScriptMessageHandlerForName:@"acceptanceGet"];
[userCC removeScriptMessageHandlerForName:@"tradeBuyGet"];
[userCC removeScriptMessageHandlerForName:@"helpCenterGet"];
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
5,iOS给JS传值
//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSLog(@"加载完成");
//加载完成后隐藏progressView
self.myProgressView.hidden = YES;
//用户名
NSString * jsStr = [NSString stringWithFormat:@"getUserName('%@')",[WDWalletManager selectedModel].name];
[self.myCustomWebView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"==%@----%@",result, error);
}];
//用户ID
NSString * jsuserIDStr = [NSString stringWithFormat:@"getUserID('%@')",[NSString stringWithFormat:@"%@",[WDWalletManager selectedModel].idnentifier]];
[self.myCustomWebView evaluateJavaScript:jsuserIDStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"==%@----%@",result, error);
}];
//图片
NSString * jsPicStr = [NSString stringWithFormat:@"getUserPic('%@')",self.userPicStr];
[self.myCustomWebView evaluateJavaScript:jsPicStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"==%@----%@",result, error);
}];
///判断iPhone X
NSString * jsIphoneXStr = [NSString stringWithFormat:@"isIphoneX('%@')",iPhoneX?@"yes":@"no"];
[self.myCustomWebView evaluateJavaScript:jsIphoneXStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"==%@----%@",result, error);
}];
}
返回的方法还是同一个地方进行判断!记住一定要告诉js的操作object不能穿空.