CustomerWebViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #import "SafeForKey.h"
  11. #import "iPhone.h"
  12. @interface CustomerWebViewController ()<WKScriptMessageHandler,WKNavigationDelegate,WKUIDelegate>
  13. @property (nonatomic,strong) WKWebView *webView;
  14. @end
  15. @implementation CustomerWebViewController
  16. @synthesize titleStr;
  17. @synthesize needHidenNav;
  18. -(void)viewWillAppear:(BOOL)animated {
  19. [super viewWillAppear:animated];
  20. [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"share"];
  21. }
  22. - (void)viewWillDisappear:(BOOL)animated {
  23. [super viewWillDisappear:animated];
  24. [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"share"];
  25. }
  26. - (void)viewDidAppear:(BOOL)animated {
  27. [super viewDidAppear:animated];
  28. /**导航栏显示*/
  29. }
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. [self drawView];
  33. }
  34. - (void)drawView {
  35. [self.toolBar setHidden:YES];
  36. [self.navigationBar setHidden:YES];
  37. self.titleLabel.text = titleStr;
  38. // WKWebView
  39. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  40. self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:config];
  41. // 解决闪白问题
  42. self.view.backgroundColor = [UIColor whiteColor];
  43. self.webView.backgroundColor = [UIColor whiteColor];
  44. self.webView.scrollView.backgroundColor = [UIColor whiteColor];
  45. self.webView.opaque = NO;
  46. self.webView.navigationDelegate = self;
  47. self.webView.UIDelegate = self;
  48. //要配置点击事件
  49. [self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
  50. if (error == noErr) {
  51. if ([result isKindOfClass:[NSString class]]) {
  52. NSString *userAgent = result;
  53. NSString *appVersion = [iPhone appVersion];
  54. NSString *newUserAgent = [userAgent stringByAppendingString:[NSString stringWithFormat:@"iosVersionNumber/%@",appVersion]];
  55. [self.webView setCustomUserAgent:newUserAgent];
  56. }
  57. }
  58. }];
  59. [self.view addSubview:self.webView];
  60. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  61. if (needHidenNav){
  62. make.top.mas_equalTo(0);
  63. }else{
  64. make.top.equalTo(self.navBarBGView.mas_bottom);
  65. }
  66. make.left.mas_equalTo(0);
  67. make.width.mas_equalTo(SCREEN_W);
  68. make.bottom.mas_equalTo(0);
  69. }];
  70. // 加载网页
  71. NSURL *url = [NSURL URLWithString:self.webUrl];
  72. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  73. [self.webView loadRequest:request];
  74. if (needHidenNav){
  75. [self.navBarBGView setHidden:YES];
  76. [self.view addSubview:self.backBtn];
  77. [self.backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
  78. make.top.mas_equalTo(H_STATE_BAR + 12.f);
  79. }];
  80. }else{
  81. [self.navBarBGView setHidden:NO];
  82. }
  83. }
  84. #pragma mark 事件代理
  85. // 页面开始加载时调用
  86. -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  87. [self showWebView0];
  88. }
  89. // 当内容开始返回时调用
  90. - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
  91. }
  92. // 页面加载完成之后调用
  93. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//这里修改导航栏的标题,动态改变
  94. [self removeNewIndicator];
  95. [self showWebView1];
  96. }
  97. // 页面加载失败时调用
  98. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
  99. [self removeNewIndicator];
  100. [self showWebView3];
  101. }
  102. #pragma mark - 解决wkwebView闪白问题
  103. // 解决wkwebView闪白问题
  104. - (void)showWebView0 {
  105. self.webView.hidden = YES;
  106. [self performSelector:@selector(showWebView1) withObject:self afterDelay:1];
  107. [self showNewIndicatorWithCanBack:YES canTouch:NO];
  108. }
  109. // 1改变网页内容背景颜色
  110. - (void)showWebView1 {
  111. [self performSelector:@selector(showWebView2) withObject:self afterDelay:0.2];
  112. }
  113. // 2加载成功
  114. - (void)showWebView2 {
  115. [self removeNewIndicator];
  116. self.webView.hidden = NO;
  117. }
  118. // 3加载失败
  119. - (void)showWebView3 {
  120. [self removeNewIndicator];
  121. }
  122. // 接收到服务器跳转请求之后再执行
  123. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
  124. }
  125. // 在收到响应后,决定是否跳转
  126. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
  127. WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
  128. //这句是必须加上的,不然会异常
  129. decisionHandler(actionPolicy);
  130. NSArray *arrViewControllers = self.navigationController.viewControllers;
  131. NSInteger index = [arrViewControllers indexOfObject:self];
  132. }
  133. // 在发送请求之前,决定是否跳转
  134. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
  135. if (navigationAction.targetFrame == nil) {
  136. [webView loadRequest:navigationAction.request];
  137. }
  138. //这句是必须加上的,不然会异常
  139. decisionHandler(WKNavigationActionPolicyAllow);
  140. }
  141. #pragma mark - WKScriptMessageHandler
  142. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  143. HLog(@"%@",message.body);
  144. HLog(@"%@",message.name);
  145. [[iToast makeText:@"已接收到消息"] show];
  146. }
  147. @end