KMResourceDownload.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // KMResourceDownload.m
  3. // PDF Master DMG
  4. //
  5. // Created by wanjun on 2023/8/1.
  6. //
  7. #import "KMResourceDownload.h"
  8. #import <ZipArchive/ZipArchive.h>
  9. //#if __has_include(<DateTools/DateTools.h>)
  10. // #import <DateTools/DateTools.h>
  11. // #define HAS_DOCUMENTAI_FRAMEWORK 1
  12. //#else
  13. // #define HAS_DOCUMENTAI_FRAMEWORK 0
  14. //#endif
  15. @interface KMResourceDownload()
  16. @property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
  17. @end
  18. @implementation KMResourceDownload
  19. static KMResourceDownload *_manager;
  20. + (KMResourceDownload *)manager {
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. _manager = [[KMResourceDownload alloc] init];
  24. });
  25. return _manager;
  26. }
  27. - (void)downloadFrameworkProgress:(KMResourceDownloadProgress)currentProgress result:(KMResourceDownloadResult)result {
  28. self.downloadResultBlock = result;
  29. self.progressBlock = currentProgress;
  30. if (self.downloadTask == nil) {
  31. NSString *urlString = @"http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.bundle.zip";
  32. NSURL *url = [NSURL URLWithString:urlString];
  33. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  34. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  35. NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url];
  36. [downloadTask resume];
  37. self.downloadTask = downloadTask;
  38. } else {
  39. [self dealDownLoadResult:NO];
  40. }
  41. }
  42. // 解压 framework 文件
  43. - (void)unzipFrameworkAtURL:(NSURL *)zipURL toDestination:(NSString *)destinationPath {
  44. // 解压逻辑,使用 Foundation 框架的 NSFileManager 或其他解压库
  45. NSFileManager *fileManager = [NSFileManager defaultManager];
  46. // 根据压缩文件类型选择解压方法
  47. BOOL success = NO;
  48. if ([zipURL.path.pathExtension isEqualToString:@"zip"]) {
  49. // 使用系统自带的解压方法,需要引入 libz.tbd 库
  50. success = [SSZipArchive unzipFileAtPath:zipURL.path
  51. toDestination:destinationPath];
  52. } else {
  53. // 如果是其他类型的压缩文件,可以使用第三方库,如 SSZipArchive 等进行解压
  54. // success = [SSZipArchive unzipFileAtPath:tempFilePath toDestination:destinationPath];
  55. }
  56. if (success) {
  57. NSLog(@"File unzipped successfully!");
  58. [fileManager removeItemAtPath:zipURL.path error:nil];
  59. } else {
  60. NSLog(@"Failed to unzip file.");
  61. [self dealDownLoadResult:NO];
  62. }
  63. }
  64. // 加载 framework 文件
  65. - (void)loadFramework:(NSString *)destinationPath {
  66. NSError *error = nil;
  67. NSBundle *frameworkBundle = [NSBundle bundleWithPath:destinationPath];
  68. if (![frameworkBundle loadAndReturnError:&error]) {
  69. NSLog(@"Error loading bundle: %@", error);
  70. [self dealDownLoadResult:NO];
  71. return;
  72. }
  73. // 使用 framework 中的代码和资源
  74. //#if HAS_DOCUMENTAI_FRAMEWORK
  75. // NSDate *selectedDate = [NSDate dateWithTimeIntervalSinceNow:-24*60*60*6+100];
  76. // NSString *week = [NSDate weekTimeAgoSinceDate:selectedDate];
  77. // NSLog(@"week == %@", week);
  78. //#else
  79. // // 其他处理或者警告用户没有导入 SomeFramework
  80. // NSLog(@"没有库");
  81. //#endif
  82. }
  83. - (BOOL)documentAIBundleIsExist {
  84. NSError *error = nil;
  85. // NSString *frameworksDirectory = [[NSBundle mainBundle] privateFrameworksPath];
  86. // NSBundle *frameworkBundle = [NSBundle bundleWithPath:[frameworksDirectory stringByAppendingPathComponent:@"DocumentAI.bundle"]];
  87. NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
  88. NSString *filePath = [resourcePath stringByAppendingPathComponent:@"DocumentAI.bundle"];
  89. NSFileManager *fileManager = [NSFileManager defaultManager];
  90. if ([fileManager fileExistsAtPath:filePath]) {
  91. return YES;
  92. } else {
  93. return NO;
  94. }
  95. }
  96. - (void)cancelDownload {
  97. [self.downloadTask cancel];
  98. self.downloadTask = nil;
  99. self.progressBlock = nil;
  100. self.downloadResultBlock = nil;
  101. }
  102. - (void)dealDownLoadResult:(BOOL)isSuccess {
  103. dispatch_async(dispatch_get_main_queue(), ^{
  104. if (self.downloadResultBlock) {
  105. self.downloadResultBlock(isSuccess);
  106. [self cancelDownload];
  107. }
  108. });
  109. }
  110. #pragma mark - NSURLSessionDelegate
  111. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  112. // 计算下载进度
  113. double progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
  114. // 可以在这里更新UI或执行其他操作
  115. NSLog(@"下载进度:%f", progress);
  116. if (self.progressBlock) {
  117. self.progressBlock(progress);
  118. }
  119. }
  120. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
  121. // Move the downloaded file to APP Contents/Frameworks folder
  122. NSFileManager *fileManager = [NSFileManager defaultManager];
  123. // NSString *frameworksDirectory = [[NSBundle mainBundle] privateFrameworksPath];
  124. NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
  125. NSString *destinationPath = [resourcePath stringByAppendingPathComponent:@"DocumentAI.bundle.zip"];
  126. NSError *moveError = nil;
  127. [fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:destinationPath] error:&moveError];
  128. if (moveError) {
  129. NSLog(@"Failed to move framework: %@", moveError);
  130. [self dealDownLoadResult:NO];
  131. return;
  132. }
  133. NSLog(@"Framework downloaded and installed successfully!");
  134. // 解压 framework 文件
  135. [self unzipFrameworkAtURL:[NSURL fileURLWithPath:destinationPath] toDestination:resourcePath];
  136. [self dealDownLoadResult:YES];
  137. }
  138. @end