OpenFileViewController.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // OpenFileViewController.m
  3. // MusicGradeExam
  4. //
  5. // Created by Kyle on 2020/7/21.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "OpenFileViewController.h"
  9. #import <QuickLook/QuickLook.h>
  10. @interface OpenFileViewController ()<QLPreviewControllerDataSource, QLPreviewControllerDelegate>
  11. @property (nonatomic, strong) QLPreviewController *previewCtrl;
  12. @property (nonatomic, strong) NSURL *fileURL;
  13. @end
  14. @implementation OpenFileViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. // Do any additional setup after loading the view.
  18. [self allocTitle:@"曲目详情"];
  19. [self addChildViewController:self.previewCtrl];
  20. [self.view addSubview:self.previewCtrl.view];
  21. }
  22. - (void)viewWillAppear:(BOOL)animated {
  23. [super viewWillAppear:animated];
  24. [self requestFileSource];
  25. }
  26. - (void)requestFileSource {
  27. if ([self isFileExist:self.urlString]) { // 本地加载
  28. self.fileURL = [self getSavePath:self.urlString];
  29. //刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的url还是上一次的url
  30. [self.previewCtrl refreshCurrentPreviewItem];
  31. }
  32. else { // 网络下载
  33. [KSRequestManager downloadFileRequestWithFileUrl:self.urlString progress:^(int64_t bytesRead, int64_t totalBytes) {
  34. } success:^(NSURL * _Nonnull fileUrl) {
  35. self.fileURL = fileUrl;
  36. [self.previewCtrl refreshCurrentPreviewItem];
  37. } faliure:^(NSError * _Nonnull error) {
  38. }];
  39. }
  40. }
  41. #pragma mark ------ QLPreviewControllerDataSource
  42. - (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
  43. return self.fileURL;
  44. }
  45. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController {
  46. return 1;
  47. }
  48. - (QLPreviewController *)previewCtrl {
  49. if (!_previewCtrl) {
  50. _previewCtrl = [[QLPreviewController alloc] init];
  51. _previewCtrl.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin);
  52. _previewCtrl.delegate = self;
  53. _previewCtrl.dataSource = self;
  54. _previewCtrl.currentPreviewItemIndex = 0;
  55. _previewCtrl.navigationController.navigationBar.userInteractionEnabled = NO;
  56. }
  57. return _previewCtrl;
  58. }
  59. #pragma mark ----- 文件处理
  60. - (BOOL)saveFileWithFileName:(NSString *)fileName {
  61. NSURL *preUrl = [self getSavePath:fileName];
  62. NSString *copyPath = preUrl.path;
  63. NSFileManager *fileMamager = [NSFileManager defaultManager];
  64. NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"FileLibrary"];
  65. path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
  66. NSError *error = nil;
  67. if (![fileMamager copyItemAtPath:copyPath toPath:path error:&error]) {
  68. [fileMamager removeItemAtPath:copyPath error:nil];
  69. NSLog(@"拷贝失败:%@", error.userInfo);
  70. return NO;
  71. }
  72. else {
  73. [fileMamager removeItemAtPath:copyPath error:nil];
  74. return YES;
  75. }
  76. }
  77. - (NSURL *)getSavePath:(NSString *)fileName {
  78. // 在Documents目录下创建一个名为FileData的文件夹
  79. NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"FileLibrary"];
  80. NSLog(@"%@",path);
  81. NSFileManager *fileManager = [NSFileManager defaultManager];
  82. BOOL isDir = FALSE;
  83. BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
  84. if(!(isDirExist && isDir)) {
  85. BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  86. if(!bCreateDir){
  87. NSLog(@"创建文件夹失败!");
  88. }
  89. NSLog(@"创建文件夹成功,文件路径%@",path);
  90. }
  91. path = [path stringByAppendingPathComponent:fileName];
  92. NSLog(@"file path:%@",path);
  93. NSURL *url=[NSURL fileURLWithPath:path];
  94. return url;
  95. }
  96. //判断文件是否已经在沙盒中存在
  97. -(BOOL)isFileExist:(NSString *)fileName {
  98. NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"FileLibrary"];
  99. NSString *filePath = [path stringByAppendingPathComponent:fileName];
  100. NSFileManager *fileManager = [NSFileManager defaultManager];
  101. BOOL result = [fileManager fileExistsAtPath:filePath];
  102. return result;
  103. }
  104. /*
  105. #pragma mark - Navigation
  106. // In a storyboard-based application, you will often want to do a little preparation before navigation
  107. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  108. // Get the new view controller using [segue destinationViewController].
  109. // Pass the selected object to the new view controller.
  110. }
  111. */
  112. @end