KMResourceDownload.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. @implementation KMResourceDownload
  16. - (void)downloadFramework {
  17. NSString *urlString = @"http://test-pdf-pro.kdan.cn:3021/downloads/DateTools.framework.zip";
  18. NSURL *url = [NSURL URLWithString:urlString];
  19. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  20. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
  21. NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  22. if (error) {
  23. NSLog(@"Failed to download framework: %@", error);
  24. return;
  25. }
  26. // Move the downloaded file to APP Contents/Frameworks folder
  27. NSFileManager *fileManager = [NSFileManager defaultManager];
  28. NSString *frameworksDirectory = [[NSBundle mainBundle] privateFrameworksPath];
  29. NSString *destinationPath = [frameworksDirectory stringByAppendingPathComponent:@"DateTools.framework.zip"];
  30. NSError *moveError = nil;
  31. [fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:destinationPath] error:&moveError];
  32. if (moveError) {
  33. NSLog(@"Failed to move framework: %@", moveError);
  34. return;
  35. }
  36. NSLog(@"Framework downloaded and installed successfully!");
  37. // 解压 framework 文件
  38. [self unzipFrameworkAtURL:[NSURL fileURLWithPath:destinationPath] toDestination:frameworksDirectory];
  39. // 加载 framework 文件
  40. [self loadFramework:[frameworksDirectory stringByAppendingPathComponent:@"DateTools.framework"]];
  41. }];
  42. [downloadTask resume];
  43. }
  44. // 解压 framework 文件
  45. - (void)unzipFrameworkAtURL:(NSURL *)zipURL toDestination:(NSString *)destinationPath {
  46. // 解压逻辑,使用 Foundation 框架的 NSFileManager 或其他解压库
  47. NSFileManager *fileManager = [NSFileManager defaultManager];
  48. // 根据压缩文件类型选择解压方法
  49. BOOL success = NO;
  50. if ([zipURL.path.pathExtension isEqualToString:@"zip"]) {
  51. // 使用系统自带的解压方法,需要引入 libz.tbd 库
  52. success = [SSZipArchive unzipFileAtPath:zipURL.path
  53. toDestination:destinationPath];
  54. } else {
  55. // 如果是其他类型的压缩文件,可以使用第三方库,如 SSZipArchive 等进行解压
  56. // success = [SSZipArchive unzipFileAtPath:tempFilePath toDestination:destinationPath];
  57. }
  58. if (success) {
  59. NSLog(@"File unzipped successfully!");
  60. [fileManager removeItemAtPath:zipURL.path error:nil];
  61. } else {
  62. NSLog(@"Failed to unzip file.");
  63. }
  64. }
  65. // 加载 framework 文件
  66. - (void)loadFramework:(NSString *)destinationPath {
  67. NSError *error = nil;
  68. // NSURL *frameworkURL = [NSURL URLWithString:destinationPath]; // 指向解压后的 framework 文件目录
  69. // NSString *frameworkPath = [[NSBundle mainBundle] pathForResource:@"DateTools" ofType:@"framework"];
  70. NSBundle *frameworkBundle = [NSBundle bundleWithPath:destinationPath];
  71. if (![frameworkBundle loadAndReturnError:&error]) {
  72. NSLog(@"Error loading framework: %@", error);
  73. return;
  74. }
  75. // 使用 framework 中的代码和资源
  76. #if HAS_DOCUMENTAI_FRAMEWORK
  77. NSDate *selectedDate = [NSDate dateWithTimeIntervalSinceNow:-24*60*60*6+100];
  78. NSString *week = [NSDate weekTimeAgoSinceDate:selectedDate];
  79. NSLog(@"week == %@", week);
  80. #else
  81. // 其他处理或者警告用户没有导入 SomeFramework
  82. NSLog(@"没有库");
  83. #endif
  84. }
  85. @end