123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- //
- // CustomerWebViewController.m
- // VclustersGemini
- //
- // Created by 余衡武 on 2022/8/11.
- // Copyright © 2022 APPLE. All rights reserved.
- //
- #import "CustomerWebViewController.h"
- #import <WebKit/WebKit.h>
- @interface CustomerWebViewController ()<WKScriptMessageHandler,WKNavigationDelegate,WKUIDelegate>
- @property (nonatomic,strong) WKWebView *webView;
- @end
- @implementation CustomerWebViewController
- -(void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
-
- /**导航栏显示*/
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self drawView];
- }
- - (void)drawView {
-
- [self.toolBar setHidden:YES];
- [self.navigationBar setHidden:YES];
- [self.navBarBGView setHidden:NO];
- self.titleLabel.text = @"隐形空间客服";
-
- // WKWebView
- WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
- self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:config];
- // 解决闪白问题
- self.view.backgroundColor = [UIColor whiteColor];
- self.webView.backgroundColor = [UIColor whiteColor];
- self.webView.scrollView.backgroundColor = [UIColor whiteColor];
- self.webView.opaque = NO;
- self.webView.navigationDelegate = self;
- self.webView.UIDelegate = self;
- [self.view addSubview:self.webView];
- [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.navBarBGView.mas_bottom);
- make.left.mas_equalTo(0);
- make.width.mas_equalTo(SCREEN_W);
- make.bottom.mas_equalTo(0);
- }];
-
- // 加载网页
- NSURL *url = [NSURL URLWithString:self.webUrl];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- [self.webView loadRequest:request];
- }
- #pragma mark 事件代理
- // 页面开始加载时调用
- -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
- [self showWebView0];
- }
- // 当内容开始返回时调用
- - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
-
- }
- // 页面加载完成之后调用
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//这里修改导航栏的标题,动态改变
- [self removeNewIndicator];
-
- [self showWebView1];
- }
- // 页面加载失败时调用
- - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
- [self removeNewIndicator];
- [self showWebView3];
- }
- #pragma mark - 解决wkwebView闪白问题
- // 解决wkwebView闪白问题
- - (void)showWebView0 {
-
- self.webView.hidden = YES;
-
- [self performSelector:@selector(showWebView1) withObject:self afterDelay:1];
-
- [self showNewIndicatorWithCanBack:YES canTouch:NO];
- }
-
- // 1改变网页内容背景颜色
- - (void)showWebView1 {
- [self performSelector:@selector(showWebView2) withObject:self afterDelay:0.2];
- }
- // 2加载成功
- - (void)showWebView2 {
- [self removeNewIndicator];
- self.webView.hidden = NO;
- }
- // 3加载失败
- - (void)showWebView3 {
- [self removeNewIndicator];
- }
- // 接收到服务器跳转请求之后再执行
- - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
-
- }
- // 在收到响应后,决定是否跳转
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
- WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
- //这句是必须加上的,不然会异常
- decisionHandler(actionPolicy);
-
- NSArray *arrViewControllers = self.navigationController.viewControllers;
- NSInteger index = [arrViewControllers indexOfObject:self];
- }
- // 在发送请求之前,决定是否跳转
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
-
- if (navigationAction.targetFrame == nil) {
- [webView loadRequest:navigationAction.request];
- }
-
- //这句是必须加上的,不然会异常
- decisionHandler(WKNavigationActionPolicyAllow);
- }
- #pragma mark - WKScriptMessageHandler
- - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
- HLog(@"%@",message.body);
- HLog(@"%@",message.name);
- }
- @end
|