CustomerWebViewController.m 6.0 KB

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