WKWebView的使用以及与js 的交互

今天重新总结一下WKWebView的用法吧  是我自己踩过的坑 以及一些自己的经验 我只是简单的用一下互相传参数

#import <WebKit/WebKit.h>   // 头文件 必不可少

<WKUIDelegate,WKScriptMessageHandler,WKNavigationDelegate>  // 遵循代理

//全局的

@property (nonatomic, strong) WKWebView *myWebView;

@property (strong, nonatomic) UIProgressView*progressView;

@property (nonatomic, strong) WKWebViewConfiguration *configuration;

接下来就是配置了 我是用 Masonry 做的尺寸

_configuration  = [[WKWebViewConfiguration alloc] init];

    _configuration.userContentController = [WKUserContentController new];

[_configuration.userContentController addScriptMessageHandler:self name:@"这个是你和Web 端商量好的方法名"]; //这里 一定要注册你们的方法 

    self.myWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:_configuration];

    self.myWebView.UIDelegate = self;

    self.myWebView.navigationDelegate = self;

    NSString * urlstring = @"你们的URL"

    NSURL* url = [NSURLURLWithString:urlstring];

    NSURLRequest *request =[NSURLRequest requestWithURL:url];

    [self.myWebViewloadRequest:request];

    [self.view addSubview:self.myWebView];

    [self.view addSubview:self.progressView];

    [self.myWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];

    [self.myWebView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.view);

    }];

这是OC 给Web传值的方法 可以写在任何地方 但注意调用的时候一定是在页面加载完之后才有用

 NSString* javaScriptString = [NSStringstringWithFormat:@"这个是你和Web 端商量好的方法名('%@')",paraStr];

    [self.myWebViewevaluateJavaScript:javaScriptStringcompletionHandler:^(id_Nullableresult,NSError*_Nullableerror) {

        NSLog(@"打印出来的结果%@----打印出来的error%@",result, error);

    }];

这个是代理方法 这个是在Web给OC传值的时候用的方法

#pragma mark 这个是webview 的代理方法

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

    if([message.nameisEqualToString:@"这个是你和Web 端商量好的方法名"]) {

        [self showMessageWithParams:message.body];

    }

}

#pragma mark 这个是交互方法

- (void)showMessageWithParams:(NSDictionary*)dict {

    if (![dict isKindOfClass:[NSDictionary class]]) {

        return;

    }

//    NSLog(@"%@",dict);

}

这个是进度条

#pragma mark- KVO监听

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

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

        self.progressView.progress = self.myWebView.estimatedProgress;

        // 加载完成

        if(self.myWebView.estimatedProgress  >=1.0f) {

            [UIView animateWithDuration:0.25f animations:^{

                self.progressView.alpha=0.0f;

                self.progressView.progress=0.0f;

            }];

        }else{

            self.progressView.alpha=1.0f;

        }

    }

}

 懒加载部分

- (WKWebView*)myWebView {

    if (!_myWebView) {

        _myWebView = [[WKWebView alloc] init];

        _myWebView.backgroundColor = [UIColor whiteColor];

        _myWebView.scrollView.showsVerticalScrollIndicator = NO;

        _myWebView.scrollView.showsHorizontalScrollIndicator = NO;

        _myWebView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64);

    }

    return _myWebView;

}

- (UIProgressView*)progressView {

    if (!_progressView) {

        _progressView = [[UIProgressView alloc] init];

        _progressView.frame = CGRectMake(0, getRectNavAndStatusHight, [UIScreen mainScreen].bounds.size.width, 2);

        _progressView.progressTintColor = [UIColor blueColor];

        _progressView.trackTintColor =[UIColor whiteColor];

    }

    return _progressView;

}

//一定要在控制器销毁的时候 释放掉

- (void)dealloc {

    [self.myWebView.configuration.userContentController removeScriptMessageHandlerForName:@"这个是你和Web 端商量好的方法名"]; //释放掉 你和Web 端商量好的方法名

    [self.myWebView removeObserver:self forKeyPath:@"estimatedProgress" context:nil];

}

这里要注意的是 我们的H5 写的时候 一定要让加window.方法名(参数),一定要这么写 

这个就是我用的时候遇到的问题  还有更多的问题 请大神指教

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言: web页面和app的直接的交互是很常见的东西,在ios8之前,用的是uiwebview,但是在ios8之后...
    qingchen91阅读 8,054评论 6 25
  • 随着H5技术的兴起,在iOS开发过程中,难免会遇到原生应用需要和H5页面交互的问题。其中会涉及方法调用及参数传值等...
    Chris_js阅读 8,302评论 1 8
  • 通过本篇文章,至少可以学习到: OC如何给JS注入对象及JS如何给IOS发送数据 JS调用alert、confir...
    一个叫小强的程序猿阅读 4,840评论 2 0
  • 我家在北面那排房子中间栋的三楼,房子采光很好,打开客厅、卧室、阳台的窗户,阳光透过窗户照到房间里来,光照就很充足,...
    绎昔阅读 2,563评论 0 0
  • 沙米拉,在法国出生,今年25岁,是一家咖啡馆的女招待。她有着一头红色的短发,搭配她每一件黑色的外套都很好看。晚上下...
    吕灵溪阅读 2,828评论 0 0

友情链接更多精彩内容