CustomerWebViewController.m 9.0 KB

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