WKWebView的使用以及与js 的交互

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

#import   // 头文件 必不可少

 // 遵循代理

//全局的

@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.方法名(参数),一定要这么写 

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

你可能感兴趣的:(WKWebView的使用以及与js 的交互)