前言:

在iOS开发中,或多或少的会嵌入一些H5页面,有时候需要原生代码和H5页面进行交互。iOS8开始苹果推出性能更强大的WKWebView,所以一下方法是关于WKWebView与JS的交互。

创建WKWebView:

遵守协议

<WKNavigationDelegate, WKUIDelegate, WKScriptMessageHandler>

-(WKWebView *)wkwebview

{

if (!_wkwebview) {

_wkwebview = [[WKWebView alloc] initWithFrame:CGRectMake(0, HeightSignal, ScreenWidth, ScreenHeight-HeightSignal-HeightBottomSafe)];

_wkwebview.scrollView.backgroundColor = [UIColor whiteColor];

_wkwebview.navigationDelegate = self;

_wkwebview.UIDelegate = self;

[self.view addSubview:_wkwebview];

}

return _wkwebview;

}

顶部网页加载进度条:

self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, HeightSignal, [[UIScreen mainScreen] bounds].size.width, 2)];

self.progressView.backgroundColor = [UIColor blueColor];

//设置进度条的高度,下面这句代码表示进度条的宽度变为原来的1倍,高度变为原来的1.5倍.

self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);

[self.view addSubview:self.progressView];

[self.wkwebview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];//进度监听

#pragma mark 加载进度监听

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {

if ([keyPath isEqualToString:@"estimatedProgress"]) {

self.progressView.progress = self.wkwebview.estimatedProgress;

if (self.progressView.progress == 1) {

/*

*添加一个简单的动画,将progressView的Height变为1.4倍,在开始加载网页的代理中会恢复为1.5倍

*动画时长0.25s,延时0.3s后开始动画

*动画结束后将progressView隐藏

*/

__weak typeof (self)weakSelf = self;

[UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{

weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);

} completion:^(BOOL finished) {

weakSelf.progressView.hidden = YES;

}];

}

}else{

[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

}

}

添加JS事件监控:

-(void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

[self.wkwebview.configuration.userContentController addScriptMessageHandler:self name:@"share"];//分享

}

移除JS事件监控:

-(void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:animated];

[self.wkwebview.configuration.userContentController removeScriptMessageHandlerForName:@"share"];//分享

}

监听方法:

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

{

if ([message.name isEqualToString:@"share"]) {

}

}

最新文章

  1. Atitit.常见软件 数据 交换格式 标准
  2. Objective-C学习笔记-第一天(2)
  3. 转: Div与table的区别
  4. MVC 4 与WebForm 混合应用 WebApi 发布常见问题
  5. C#计算程序执行速度
  6. spring03autowire属性
  7. CSS设计指南之浮动与清除
  8. 内嵌Tomcat的Connector对象的静态代码块
  9. 一种在BIOS中嵌入应用程序的方法及实现
  10. 敏捷开发冲刺--day3
  11. 音频压缩编码 opus 附完整C++代码示例
  12. [HAOI2007]分割矩阵
  13. Ajax跨域之ContentType为application/json请求失败的问题
  14. mysql left join 左连接查询关联n多张表
  15. koa-session 记录当前会话内容
  16. python 对象存储OSS 阿里云
  17. JAVA之Math类常用数学运算记录
  18. Fibratus:一款功能强大的Windows内核漏洞利用和跟踪工具
  19. celipse关联hadoop源码
  20. jira 插件介绍地址

热门文章

  1. redis数据备份与恢复
  2. synchronized 是可重入锁吗?为什么?
  3. session简介与生命周期
  4. PHP二维数组按某个字段排序
  5. 利用MySQL游标进行计算排名
  6. Python学习(三十七)—— 模板语言之自定义filter和中间件
  7. Kubernetes国内镜像、下载安装包和拉取gcr.io镜像
  8. springboot的jar包
  9. Java-正则使用
  10. SQL server 使用 内联结(INNER JOIN) 联结多个表 (以及过滤条件 WHERE, AND使用区别)