先:WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler

   // 创建一个webiview的配置项

        WKWebViewConfiguration *configuretion = [[WKWebViewConfiguration alloc] init];

         // Webview的偏好设置

         //.WKPreferences()

         // 设置偏好设置

         configuretion.preferences = [[WKPreferences alloc]init];

         configuretion.preferences.minimumFontSize = ;

         configuretion.preferences.javaScriptEnabled = true;

         configuretion.processPool = [[WKProcessPool alloc]init];

         // 通过js与webview内容交互配置

         configuretion.userContentController = [[WKUserContentController alloc] init];

         [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

         //OC注册供JS调用的方法

         [ configuretion.userContentController addScriptMessageHandler:self name:@"ABC"];

         // 默认是不能通过JS自动打开窗口的,必须通过用户交互才能打开

         configuretion.preferences.javaScriptCanOpenWindowsAutomatically = NO;

         _wkwebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuretion];//self.view.bounds

         [_wkwebView goBack];

         [_wkwebView goForward];

         _wkwebView.navigationDelegate = self;

         _wkwebView.UIDelegate = self;

         NSURL *url = [NSURL URLWithString:urlStr];  //测试本地H5

         NSURLRequest *request = [NSURLRequest requestWithURL:url];

         [_wkwebView loadRequest:request];

再:

 -(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{

     // userContentController 注册message的WKUserContentController;
// message:js传过来的数据
// id body:消息携带的信息 Allowed types are NSNumber, NSString, NSDate, NSArray, NSDictionary, and NSNull.
// NSString *name;//消息的名字 如aaa
//message.name js发送的方法名称 if([message.name isEqualToString:@"ABC"]){ NSString * body = [message.body objectForKey:@"body"]; //在这里写oc 实现协议的native方法 } }
 前端h5代码:前端需要用 window.webkit.messageHandlers.注册的方法名.postMessage({body:传输的数据} 来给native发送消息

 例如:

 function secondClick() {

     window.webkit.messageHandlers.aaa.postMessage({body: 'call js alert in js'});

 }

 **重要 如果注册了方法    [userContentController addScriptMessageHandler:self  name:@“aaa"];

 会导致hangler一直被引用 导致不走Delloc web页面无法释放 所以要在-(void)viewDidDisappear:(BOOL)animated中将messageHandler移除

     [userContentController removeScriptMessageHandlerForName:@“aaa"]; 关闭web页时会释放内存。

最新文章

  1. 深入理解Java:内部类
  2. Unity使用Windows弹窗保存图片
  3. Win10专业版激活永久可查激活信息
  4. webBroser获取cookie
  5. 关于sharepoint 2010无法显示用户中文名的解决方法和详细剖析
  6. Java基础知识强化之IO流笔记29:BufferedOutputStream / BufferedInputStream(字节缓冲流)之BufferedInputStream读取数据
  7. safari的input问题
  8. 36.java_exception_test
  9. cocos2d-x项目与vs2013编译
  10. 使用JWT的OAuth2的SSO分析
  11. asp.netMVC4使用Bootstrap4
  12. css margin使用技巧
  13. javaEE练习(商城练习)
  14. sql server 性能调优之 资源等待 LCk
  15. ELK 环境搭建4-Kafka + zookeeper
  16. Python3基础语法你学会了么
  17. oracle expdp/impdp 用法详解
  18. Docker Dockerfile 基本结构详解
  19. Postgresql 连接更新
  20. 读EntityFramework.DynamicFilters源码_心得_设计思想_04

热门文章

  1. swift 动画合集
  2. SqlServer Where后面Case When语句的写法
  3. An exception occurred while acquiring a poolable resource. Will retry.
  4. 块级格式化上下文(block formatting context)
  5. GsonUtils.getGson().fromJson() 转泛型集合
  6. @UniqueConstraint
  7. [转载]jquery中attr和prop的区别
  8. MAC安装M2Crypto报command 'cc' failed with exit status 1
  9. JavaEE知识点总结
  10. 用贝赛尔曲线把图片, 按钮, label 绘成圆 或圆角矩形