// // EMChatFileShowVC.m // smartRhino // // Created by niuzhen on 2019/12/31. // Copyright © 2019 tederen. All rights reserved. // #import "EMChatFileShowVC.h" @interface EMChatFileShowVC () @property (strong, nonatomic) WKWebView * webView; @property (weak, nonatomic) IBOutlet UILabel *titleL; @property (weak, nonatomic) IBOutlet UIView *NavBarV; @end @implementation EMChatFileShowVC +(EMChatFileShowVC *)initEMChatFileShowVC{ EMChatFileShowVC *controller = [StoryboardManager.shared.chatMsg instantiateViewControllerWithIdentifier:@"EMChatFileShowVC"]; return controller; } - (WKWebView *)webView { if (!_webView) { _webView = [[WKWebView alloc] init]; } return _webView; } - (void)DownFile { WS(weakSelf); AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer.timeoutInterval = 60; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:self.fileUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [weakSelf writeToFile:responseObject fileName:weakSelf.titleStr]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@",error); }]; } // 写入文件 -(void)writeToFile:(NSData *)data fileName:(NSString *)fileName{ NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * _filePath = [paths objectAtIndex:0]; NSString * filePath = [_filePath stringByAppendingPathComponent:fileName]; // NSData写入文件 [data writeToFile:filePath atomically:YES]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]; [self.webView loadRequest:request]; } - (void)viewDidLoad { [super viewDidLoad]; self.titleL.text = self.titleStr; [self.view addSubview:self.webView]; [self.webView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.top.mas_equalTo(self.NavBarV.mas_bottom); if (@available(iOS 11.0, *)) { make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom); } else { make.bottom.equalTo(self.view.mas_bottom); } }]; self.webView.UIDelegate = self; self.fd_prefersNavigationBarHidden = YES; self.webView.navigationDelegate = self; // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回 self.webView.allowsBackForwardNavigationGestures = YES; //可返回的页面列表, 存储已打开过的网页 // WKBackForwardList * backForwardList = [_webView backForwardList]; [self DownFile]; // NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.fileUrl]]; // // [self.webView loadRequest:request]; // //页面后退 // [self.webView goBack]; // //页面前进 // [self.webView goForward]; // //刷新当前页面 // [self.webView reload]; } - (IBAction)backAction:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } @end