美文网首页iOS开发(OC)
WKWebView与JS交互代码(OC)

WKWebView与JS交互代码(OC)

作者: 爱恨的潮汐 | 来源:发表于2017-04-21 11:39 被阅读33次
    
    #import "AboutMyViewController.h"
    #import "HtmlWKWebView.h"
    @interface AboutMyViewController ()<WKScriptMessageHandler>
    //分享
    @property (nonatomic,weak)UIView *bageView;
    @property (nonatomic,weak)UIView *contentView;
    
    @property WebViewJavascriptBridge *bridge;
    
    @property(nonatomic,strong)HtmlWKWebView * webView;
    @end
    
    @implementation AboutMyViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        self.automaticallyAdjustsScrollViewInsets = NO;
        [self creatNav];
        //创建网页
        [self createWebView];
        
    }
    -(void)creatNav{
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(pressButtonLeft)];
        self.navigationItem.leftBarButtonItem = item;
        //设置中间标题
        UILabel *label                = [[UILabel alloc]init];
        label.frame                   = CGRectMake(100, 0, self.view.frame.size.width-200, 64);
        label.text                    = @"嗅美";
        label.textColor               = [UIColor blackColor];
        label.textAlignment           = NSTextAlignmentCenter;
        label.font                    = [UIFont systemFontOfSize:18];
        self.navigationItem.titleView = label;
        
    
    }
    -(void)pressButtonLeft{
        [self dismissViewControllerAnimated:NO completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    -(void)createWebView{
        
        //进行配置控制器
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        //实例化对象
        configuration.userContentController = [WKUserContentController new];
        //与JS交互的注册,JS调用OC方法
        [configuration.userContentController addScriptMessageHandler:self name:@"appShareIOS"];
        
        //初始化
        _webView = [[HtmlWKWebView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) configuration:configuration];
        //yuH5有交互的初始化完手动调用创建UI
        _webView.actionJS = @"和JS有交互";
        //传wkWebview过去注册KVO开启进度条
        self.webView.wkWebview = _webView;
       //关闭反弹
        self.webView.scrollView.bounces = NO;
        
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",AboutXiumeiNew,GET_VERSION]]]];
        
        [self.view addSubview:_webView];
        
    }
    
    
    /**
     * JS给原生传数据:JS调用原生的方法
     */
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
        //得到JS返回的数据
    //    NSDictionary *dict = message.body;
        NSLog(@"----WKwebView交互----");
        //分享
        [self shareXiumei];
    
    }
    
    
    
    
    
    //分享
    -(void)shareXiumei{
       
        //分享
        UIImage * image = [UIImage imageNamed:@"嗅美logo"];
        [ShareView share:self ShareTitle:@"嗅美 超in美妆护肤穿衣搭配助手-看美容美妆心得分享,享正品折扣免费试用" WithContent: [NSString stringWithFormat:@"版本:%@",GET_VERSION] ShareUrl:@"http://www.xiumeiapp.com/androidToH5/shareApp.html" shareImage:image ReporStrType:nil shareType:nil completion:^(NSString *code) {
            
        }];
        
    
    }
    //注意!注意!注意:上面将当前ViewController设置为MessageHandler之后需要在当前ViewController销毁前将其移除,否则会造成内存泄漏。
    -(void)dealloc{
        
          [_webView.configuration.userContentController removeScriptMessageHandlerForName:@"appShareIOS"];
    }
    
    - (void)viewWillAppear:(BOOL)animated{
        
        [super viewWillAppear:animated];
        
        [self.navigationController setNavigationBarHidden:NO animated:NO];
        self.tabBarController.tabBar.hidden = YES;
        //友盟统计
        [MobClick beginLogPageView:[NSString stringWithFormat:@"%@", self.class]];
      
        
    }
    
    - (void)viewWillDisappear:(BOOL)animated{
        
        [super viewWillDisappear:animated];
        
        //友盟统计
        [MobClick endLogPageView:[NSString stringWithFormat:@"%@", self.class]];
      
    
    }
    
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    
    

    相关文章

      网友评论

        本文标题:WKWebView与JS交互代码(OC)

        本文链接:https://www.haomeiwen.com/subject/fjkdzttx.html