EMChatFileShowVC.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // EMChatFileShowVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2019/12/31.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "EMChatFileShowVC.h"
  9. @interface EMChatFileShowVC ()<WKUIDelegate,WKNavigationDelegate>
  10. @property (strong, nonatomic) WKWebView * webView;
  11. @property (weak, nonatomic) IBOutlet UILabel *titleL;
  12. @property (weak, nonatomic) IBOutlet UIView *NavBarV;
  13. @end
  14. @implementation EMChatFileShowVC
  15. +(EMChatFileShowVC *)initEMChatFileShowVC{
  16. EMChatFileShowVC *controller = [StoryboardManager.shared.chatMsg instantiateViewControllerWithIdentifier:@"EMChatFileShowVC"];
  17. return controller;
  18. }
  19. - (WKWebView *)webView
  20. {
  21. if (!_webView) {
  22. _webView = [[WKWebView alloc] init];
  23. }
  24. return _webView;
  25. }
  26. - (void)DownFile
  27. {
  28. WS(weakSelf);
  29. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  30. manager.requestSerializer.timeoutInterval = 60;
  31. manager.requestSerializer = [AFHTTPRequestSerializer serializer];
  32. manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  33. [manager GET:self.fileUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  34. [weakSelf writeToFile:responseObject fileName:weakSelf.titleStr];
  35. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  36. NSLog(@"%@",error);
  37. }];
  38. }
  39. // 写入文件
  40. -(void)writeToFile:(NSData *)data fileName:(NSString *)fileName{
  41. NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  42. NSString * _filePath = [paths objectAtIndex:0];
  43. NSString * filePath = [_filePath stringByAppendingPathComponent:fileName];
  44. // NSData写入文件
  45. [data writeToFile:filePath atomically:YES];
  46. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]];
  47. [self.webView loadRequest:request];
  48. }
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. self.titleL.text = self.titleStr;
  52. [self.view addSubview:self.webView];
  53. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.left.right.mas_equalTo(self.view);
  55. make.top.mas_equalTo(self.NavBarV.mas_bottom);
  56. if (@available(iOS 11.0, *)) {
  57. make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
  58. } else {
  59. make.bottom.equalTo(self.view.mas_bottom);
  60. }
  61. }];
  62. self.webView.UIDelegate = self;
  63. self.fd_prefersNavigationBarHidden = YES;
  64. self.webView.navigationDelegate = self;
  65. // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回
  66. self.webView.allowsBackForwardNavigationGestures = YES;
  67. //可返回的页面列表, 存储已打开过的网页
  68. // WKBackForwardList * backForwardList = [_webView backForwardList];
  69. [self DownFile];
  70. // NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.fileUrl]];
  71. //
  72. // [self.webView loadRequest:request];
  73. // //页面后退
  74. // [self.webView goBack];
  75. // //页面前进
  76. // [self.webView goForward];
  77. // //刷新当前页面
  78. // [self.webView reload];
  79. }
  80. - (IBAction)backAction:(id)sender {
  81. [self.navigationController popViewControllerAnimated:YES];
  82. }
  83. @end