CustomerWebViewController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // CustomerWebViewController.m
  3. // VclustersGemini
  4. //
  5. // Created by 余衡武 on 2022/8/11.
  6. // Copyright © 2022 APPLE. All rights reserved.
  7. //
  8. #import "CustomerWebViewController.h"
  9. #import <WebKit/WebKit.h>
  10. @interface CustomerWebViewController ()<WKScriptMessageHandler,WKNavigationDelegate,WKUIDelegate>
  11. @property (nonatomic,strong) WKWebView *webView;
  12. @end
  13. @implementation CustomerWebViewController
  14. -(void)viewWillAppear:(BOOL)animated {
  15. [super viewWillAppear:animated];
  16. }
  17. - (void)viewWillDisappear:(BOOL)animated {
  18. [super viewWillDisappear:animated];
  19. }
  20. - (void)viewDidAppear:(BOOL)animated {
  21. [super viewDidAppear:animated];
  22. /**导航栏显示*/
  23. }
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self drawView];
  27. }
  28. - (void)drawView {
  29. [self.toolBar setHidden:YES];
  30. [self.navigationBar setHidden:YES];
  31. [self.navBarBGView setHidden:NO];
  32. self.titleLabel.text = @"隐形空间客服";
  33. // WKWebView
  34. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  35. self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:config];
  36. // 解决闪白问题
  37. self.view.backgroundColor = [UIColor whiteColor];
  38. self.webView.backgroundColor = [UIColor whiteColor];
  39. self.webView.scrollView.backgroundColor = [UIColor whiteColor];
  40. self.webView.opaque = NO;
  41. self.webView.navigationDelegate = self;
  42. self.webView.UIDelegate = self;
  43. [self.view addSubview:self.webView];
  44. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.top.equalTo(self.navBarBGView.mas_bottom);
  46. make.left.mas_equalTo(0);
  47. make.width.mas_equalTo(SCREEN_W);
  48. make.bottom.mas_equalTo(0);
  49. }];
  50. // 加载网页
  51. NSURL *url = [NSURL URLWithString:self.webUrl];
  52. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  53. [self.webView loadRequest:request];
  54. }
  55. #pragma mark 事件代理
  56. // 页面开始加载时调用
  57. -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  58. [self showWebView0];
  59. }
  60. // 当内容开始返回时调用
  61. - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
  62. }
  63. // 页面加载完成之后调用
  64. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//这里修改导航栏的标题,动态改变
  65. [self removeNewIndicator];
  66. [self showWebView1];
  67. }
  68. // 页面加载失败时调用
  69. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
  70. [self removeNewIndicator];
  71. [self showWebView3];
  72. }
  73. #pragma mark - 解决wkwebView闪白问题
  74. // 解决wkwebView闪白问题
  75. - (void)showWebView0 {
  76. self.webView.hidden = YES;
  77. [self performSelector:@selector(showWebView1) withObject:self afterDelay:1];
  78. [self showNewIndicatorWithCanBack:YES canTouch:NO];
  79. }
  80. // 1改变网页内容背景颜色
  81. - (void)showWebView1 {
  82. [self performSelector:@selector(showWebView2) withObject:self afterDelay:0.2];
  83. }
  84. // 2加载成功
  85. - (void)showWebView2 {
  86. [self removeNewIndicator];
  87. self.webView.hidden = NO;
  88. }
  89. // 3加载失败
  90. - (void)showWebView3 {
  91. [self removeNewIndicator];
  92. }
  93. // 接收到服务器跳转请求之后再执行
  94. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
  95. }
  96. // 在收到响应后,决定是否跳转
  97. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
  98. WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
  99. //这句是必须加上的,不然会异常
  100. decisionHandler(actionPolicy);
  101. NSArray *arrViewControllers = self.navigationController.viewControllers;
  102. NSInteger index = [arrViewControllers indexOfObject:self];
  103. }
  104. // 在发送请求之前,决定是否跳转
  105. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
  106. if (navigationAction.targetFrame == nil) {
  107. [webView loadRequest:navigationAction.request];
  108. }
  109. //这句是必须加上的,不然会异常
  110. decisionHandler(WKNavigationActionPolicyAllow);
  111. }
  112. #pragma mark - WKScriptMessageHandler
  113. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  114. HLog(@"%@",message.body);
  115. HLog(@"%@",message.name);
  116. }
  117. @end