iOS中WKWebView 与JS交互

//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不能穿空.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值