123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- //
- // CustomerWebViewController.m
- // VclustersGemini
- //
- // Created by 余衡武 on 2022/8/11.
- // Copyright © 2022 APPLE. All rights reserved.
- //
- #import "CustomerWebViewController.h"
- #import <WebKit/WebKit.h>
- #import "SafeForKey.h"
- #import "iPhone.h"
- #import "fileUploadToFileCenterModel.h"
- #import <SSZipArchive/SSZipArchive.h>
- @interface CustomerWebViewController ()<WKScriptMessageHandler,WKNavigationDelegate,WKUIDelegate>
- @property (nonatomic,strong) WKWebView *webView;
- @end
- @implementation CustomerWebViewController
- @synthesize titleStr;
- @synthesize needHidenNav;
- -(void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"share"];
- [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"getFileKey"];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"share"];
- [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"getFileKey"];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
-
- /**导航栏显示*/
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self drawView];
- }
- #pragma mark 点击返回
- - (void)backBtnPressed
- {
- if([self.webView canGoBack] && ![self.webUrl containsString:@"novice?isNovice=true"]){
- [self.webView goBack];
- }
- else {
- if(_didClickBackFun){
- _didClickBackFun();
- }
- [super backBtnPressed];
- }
- }
- - (void)drawView {
-
- [self.toolBar setHidden:YES];
- [self.navigationBar setHidden:YES];
- self.titleLabel.text = titleStr;
-
- // WKWebView
- WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
- self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:config];
- // 解决闪白问题
- self.view.backgroundColor = [UIColor whiteColor];
- self.webView.backgroundColor = [UIColor whiteColor];
- self.webView.scrollView.backgroundColor = [UIColor whiteColor];
- self.webView.opaque = NO;
- self.webView.navigationDelegate = self;
- self.webView.UIDelegate = self;
-
- //要配置点击事件
- [self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
- if (error == noErr) {
- if ([result isKindOfClass:[NSString class]]) {
- NSString *userAgent = result;
- NSString *appVersion = [iPhone appVersion];
- NSString *newUserAgent = [userAgent stringByAppendingString:[NSString stringWithFormat:@"iosVersionNumber/%@",appVersion]];
- [self.webView setCustomUserAgent:newUserAgent];
- }
- }
- }];
-
- [self.view addSubview:self.webView];
- [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
- if (needHidenNav){
- make.top.mas_equalTo(0);
- }else{
- make.top.equalTo(self.navBarBGView.mas_bottom);
- }
- make.left.mas_equalTo(0);
- make.width.mas_equalTo(SCREEN_W);
- make.bottom.mas_equalTo(0);
- }];
- // 加载网页
- NSURL *url = [NSURL URLWithString:self.webUrl];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- [self.webView loadRequest:request];
-
- if (needHidenNav){
- [self.navBarBGView setHidden:YES];
- [self.view addSubview:self.backBtn];
- [self.backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(H_STATE_BAR + 12.f);
- }];
- }else{
- [self.navBarBGView setHidden:NO];
- }
- }
- //- (void)setWebUrl:(NSString *)webUrl{
- // _webUrl = webUrl;
- //
- // // 加载网页
- // NSURL *url = [NSURL URLWithString:_webUrl];
- // NSURLRequest *request = [NSURLRequest requestWithURL:url];
- // [self.webView loadRequest:request];
- //
- //}
- #pragma mark 事件代理
- // 页面开始加载时调用
- -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
- [self showWebView0];
- }
- // 当内容开始返回时调用
- - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
-
- }
- // 页面加载完成之后调用
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//这里修改导航栏的标题,动态改变
- [self removeNewIndicator];
-
- NSString *curWebTitle = webView.title;
-
- //暂时只处理 首页点进来的新手引导
- if(curWebTitle && curWebTitle.length>0 && [self.webUrl containsString:@"novice?isNovice=true"]){
- self.titleLabel.text = curWebTitle;
- }
-
- [self showWebView1];
- }
- // 页面加载失败时调用
- - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
- [self removeNewIndicator];
- [self showWebView3];
- }
- #pragma mark - 解决wkwebView闪白问题
- // 解决wkwebView闪白问题
- - (void)showWebView0 {
-
- self.webView.hidden = YES;
-
- [self performSelector:@selector(showWebView1) withObject:self afterDelay:1];
-
- [self showNewIndicatorWithCanBack:YES canTouch:NO];
- }
-
- // 1改变网页内容背景颜色
- - (void)showWebView1 {
- [self performSelector:@selector(showWebView2) withObject:self afterDelay:0.2];
- }
- // 2加载成功
- - (void)showWebView2 {
- [self removeNewIndicator];
- self.webView.hidden = NO;
- }
- // 3加载失败
- - (void)showWebView3 {
- [self removeNewIndicator];
- }
- // 接收到服务器跳转请求之后再执行
- - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
-
- }
- // 在收到响应后,决定是否跳转
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
- WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
- //这句是必须加上的,不然会异常
- decisionHandler(actionPolicy);
-
- // NSArray *arrViewControllers = self.navigationController.viewControllers;
- // NSInteger index = [arrViewControllers indexOfObject:self];
- }
- // 在发送请求之前,决定是否跳转
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
-
- if (navigationAction.targetFrame == nil) {
- [webView loadRequest:navigationAction.request];
- }
-
- //这句是必须加上的,不然会异常
- decisionHandler(WKNavigationActionPolicyAllow);
- }
- #pragma mark - WKScriptMessageHandler
- - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
- HLog(@"%@",message.body);
- HLog(@"%@",message.name);
- //[[iToast makeText:@"已接收到消息"] show];
-
-
- if([message.name isEqualToString:@"getFileKey"])
- {//帮助反馈 h5调用原生 需要日志的FileKey
- [self gotoAddLogFun];
- }
- }
- #pragma mark 获取瑞云日志上传到文件中心
- //1.读取日志
- //2.上传到文件中心
- //3.拿到key上传到盒子服务器
- - (void)gotoAddLogFun
- {
-
- NSString *zipPath = [NSString stringWithFormat:@"%@/logs/archive.zip",CachesPatch];
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- // 检查文件是否存在
- BOOL fileExists = [fileManager fileExistsAtPath:zipPath];
- if (fileExists) {
- // 删除文件
- NSError *error = nil;
- BOOL success = [fileManager removeItemAtPath:zipPath error:&error];
- if (success) {
- HLog(@"文件已成功删除: %@", zipPath);
- } else {
- HLog(@"删除文件失败: %@", [error localizedDescription]);
- }
- }
-
- //2.读取日志
- //NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/debug_0.log",CachesPatch];
- NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/webrtc_log_0",CachesPatch];
-
- NSString *ruiyunLogPath2 = [NSString stringWithFormat:@"%@/logs/app.log",CachesPatch];
-
- //3. 将文件压缩成ZIP
- NSArray *filesToZip = @[ruiyunLogPath, ruiyunLogPath2];
- [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:filesToZip];
-
- NSData *logData = [NSData dataWithContentsOfFile:zipPath];
- //HLog(@"%@",logData);
-
- if(!logData || [logData length]==0){
- //1.4.2以及后续 webrtc版本 没有日志
- [self getFileKeyToH5By:@""];
- return;
- }
-
- //4.上传到文件中心
- NSMutableDictionary *paraDict = [NSMutableDictionary new];
-
- NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
- if(!snStr){
- snStr = [iTools getNowTimeString];
- }
- NSString*filename = [[NSString alloc] initWithFormat:@"%@_ios.log",snStr];
-
- [paraDict setValue:filename forKey:@"filename"];
- [paraDict setValue:@7 forKey:@"days"];
-
- KWeakSelf
- [[netWorkManager shareInstance] doUploadFileToFileServiceWithParams:paraDict data:logData success:^(id _Nonnull responseObject) {
- HLog(@"%@",responseObject);
-
- fileUploadToFileCenterModel *model = [[fileUploadToFileCenterModel alloc] initWithDictionary:responseObject error:nil];
- if(model.code == 200){
- NSString * fileKey = model.data.fileKey;
- if(fileKey && fileKey.length >0){
- [weakSelf getFileKeyToH5By:fileKey];
- }
- else{
- [weakSelf getFileKeyToH5By:@""];
- }
- }
- else{
- [weakSelf getFileKeyToH5By:@""];
- }
-
- } faild:^(NSError * _Nonnull error) {
- [weakSelf getFileKeyToH5By:@""];
- }];
- }
- #pragma mark 拿到文件中心的key给h5
- - (void)getFileKeyToH5By:(NSString*)fileKey
- {
- NSString *jsFunction = @"transferFileKey('%@');";
- jsFunction = [jsFunction stringByReplacingOccurrencesOfString:@"%@" withString:fileKey];
- [self.webView evaluateJavaScript:jsFunction completionHandler:^(id _Nullable result, NSError * _Nullable error) {
- if (error != nil) {
- NSLog(@"Error evaluating JavaScript: %@", error);
- }
- }];
-
- }
- @end
|