CustomerWebViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #import "fileUploadToFileCenterModel.h"
  13. @interface CustomerWebViewController ()<WKScriptMessageHandler,WKNavigationDelegate,WKUIDelegate>
  14. @property (nonatomic,strong) WKWebView *webView;
  15. @end
  16. @implementation CustomerWebViewController
  17. @synthesize titleStr;
  18. @synthesize needHidenNav;
  19. -(void)viewWillAppear:(BOOL)animated {
  20. [super viewWillAppear:animated];
  21. [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"share"];
  22. [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"getFileKey"];
  23. }
  24. - (void)viewWillDisappear:(BOOL)animated {
  25. [super viewWillDisappear:animated];
  26. [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"share"];
  27. [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"getFileKey"];
  28. }
  29. - (void)viewDidAppear:(BOOL)animated {
  30. [super viewDidAppear:animated];
  31. /**导航栏显示*/
  32. }
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. [self drawView];
  36. }
  37. #pragma mark 点击返回
  38. - (void)backBtnPressed
  39. {
  40. if([self.webView canGoBack]){
  41. [self.webView goBack];
  42. }
  43. else {
  44. [super backBtnPressed];
  45. }
  46. }
  47. - (void)drawView {
  48. [self.toolBar setHidden:YES];
  49. [self.navigationBar setHidden:YES];
  50. self.titleLabel.text = titleStr;
  51. // WKWebView
  52. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  53. self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:config];
  54. // 解决闪白问题
  55. self.view.backgroundColor = [UIColor whiteColor];
  56. self.webView.backgroundColor = [UIColor whiteColor];
  57. self.webView.scrollView.backgroundColor = [UIColor whiteColor];
  58. self.webView.opaque = NO;
  59. self.webView.navigationDelegate = self;
  60. self.webView.UIDelegate = self;
  61. //要配置点击事件
  62. [self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
  63. if (error == noErr) {
  64. if ([result isKindOfClass:[NSString class]]) {
  65. NSString *userAgent = result;
  66. NSString *appVersion = [iPhone appVersion];
  67. NSString *newUserAgent = [userAgent stringByAppendingString:[NSString stringWithFormat:@"iosVersionNumber/%@",appVersion]];
  68. [self.webView setCustomUserAgent:newUserAgent];
  69. }
  70. }
  71. }];
  72. [self.view addSubview:self.webView];
  73. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  74. if (needHidenNav){
  75. make.top.mas_equalTo(0);
  76. }else{
  77. make.top.equalTo(self.navBarBGView.mas_bottom);
  78. }
  79. make.left.mas_equalTo(0);
  80. make.width.mas_equalTo(SCREEN_W);
  81. make.bottom.mas_equalTo(0);
  82. }];
  83. // 加载网页
  84. NSURL *url = [NSURL URLWithString:self.webUrl];
  85. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  86. [self.webView loadRequest:request];
  87. if (needHidenNav){
  88. [self.navBarBGView setHidden:YES];
  89. [self.view addSubview:self.backBtn];
  90. [self.backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
  91. make.top.mas_equalTo(H_STATE_BAR + 12.f);
  92. }];
  93. }else{
  94. [self.navBarBGView setHidden:NO];
  95. }
  96. }
  97. #pragma mark 事件代理
  98. // 页面开始加载时调用
  99. -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  100. [self showWebView0];
  101. }
  102. // 当内容开始返回时调用
  103. - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
  104. }
  105. // 页面加载完成之后调用
  106. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//这里修改导航栏的标题,动态改变
  107. [self removeNewIndicator];
  108. [self showWebView1];
  109. }
  110. // 页面加载失败时调用
  111. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
  112. [self removeNewIndicator];
  113. [self showWebView3];
  114. }
  115. #pragma mark - 解决wkwebView闪白问题
  116. // 解决wkwebView闪白问题
  117. - (void)showWebView0 {
  118. self.webView.hidden = YES;
  119. [self performSelector:@selector(showWebView1) withObject:self afterDelay:1];
  120. [self showNewIndicatorWithCanBack:YES canTouch:NO];
  121. }
  122. // 1改变网页内容背景颜色
  123. - (void)showWebView1 {
  124. [self performSelector:@selector(showWebView2) withObject:self afterDelay:0.2];
  125. }
  126. // 2加载成功
  127. - (void)showWebView2 {
  128. [self removeNewIndicator];
  129. self.webView.hidden = NO;
  130. }
  131. // 3加载失败
  132. - (void)showWebView3 {
  133. [self removeNewIndicator];
  134. }
  135. // 接收到服务器跳转请求之后再执行
  136. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
  137. }
  138. // 在收到响应后,决定是否跳转
  139. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
  140. WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
  141. //这句是必须加上的,不然会异常
  142. decisionHandler(actionPolicy);
  143. // NSArray *arrViewControllers = self.navigationController.viewControllers;
  144. // NSInteger index = [arrViewControllers indexOfObject:self];
  145. }
  146. // 在发送请求之前,决定是否跳转
  147. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
  148. if (navigationAction.targetFrame == nil) {
  149. [webView loadRequest:navigationAction.request];
  150. }
  151. //这句是必须加上的,不然会异常
  152. decisionHandler(WKNavigationActionPolicyAllow);
  153. }
  154. #pragma mark - WKScriptMessageHandler
  155. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  156. HLog(@"%@",message.body);
  157. HLog(@"%@",message.name);
  158. //[[iToast makeText:@"已接收到消息"] show];
  159. if([message.name isEqualToString:@"getFileKey"])
  160. {//帮助反馈 h5调用原生 需要日志的FileKey
  161. [self gotoAddLogFun];
  162. }
  163. }
  164. #pragma mark 获取瑞云日志上传到文件中心
  165. //1.读取日志
  166. //2.上传到文件中心
  167. //3.拿到key上传到盒子服务器
  168. - (void)gotoAddLogFun
  169. {
  170. //1.读取日志
  171. NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/debug_0.log",CachesPatch];
  172. NSData *logData = [NSData dataWithContentsOfFile:ruiyunLogPath];
  173. HLog(@"%@",logData);
  174. if(!logData || [logData length]==0){
  175. return;
  176. }
  177. //2.上传到文件中心
  178. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  179. NSString *snStr = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.changeSn;
  180. if(!snStr){
  181. snStr = [iTools getNowTimeString];
  182. }
  183. NSString*filename = [[NSString alloc] initWithFormat:@"%@_ios.log",snStr];
  184. [paraDict setValue:filename forKey:@"filename"];
  185. KWeakSelf
  186. [[netWorkManager shareInstance] doUploadFileToFileServiceWithParams:paraDict data:logData success:^(id _Nonnull responseObject) {
  187. HLog(@"%@",responseObject);
  188. fileUploadToFileCenterModel *model = [[fileUploadToFileCenterModel alloc] initWithDictionary:responseObject error:nil];
  189. if(model.code == 200){
  190. NSString * fileKey = model.data.fileKey;
  191. if(fileKey && fileKey.length >0){
  192. [weakSelf getFileKeyToH5By:fileKey];
  193. }
  194. else{
  195. [weakSelf getFileKeyToH5By:@""];
  196. }
  197. }
  198. else{
  199. [weakSelf getFileKeyToH5By:@""];
  200. }
  201. } faild:^(NSError * _Nonnull error) {
  202. [weakSelf getFileKeyToH5By:@""];
  203. }];
  204. }
  205. #pragma mark 拿到文件中心的key给h5
  206. - (void)getFileKeyToH5By:(NSString*)fileKey
  207. {
  208. NSString *jsFunction = @"transferFileKey('%@');";
  209. jsFunction = [jsFunction stringByReplacingOccurrencesOfString:@"%@" withString:fileKey];
  210. [self.webView evaluateJavaScript:jsFunction completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  211. if (error != nil) {
  212. NSLog(@"Error evaluating JavaScript: %@", error);
  213. }
  214. }];
  215. }
  216. @end