// // KMResourceDownload.m // PDF Master DMG // // Created by wanjun on 2023/8/1. // #import "KMResourceDownload.h" #import #if __has_include() #import #define HAS_DOCUMENTAI_FRAMEWORK 1 #else #define HAS_DOCUMENTAI_FRAMEWORK 0 #endif @implementation KMResourceDownload - (void)downloadFramework { NSString *urlString = @"http://test-pdf-pro.kdan.cn:3021/downloads/DateTools.framework.zip"; NSURL *url = [NSURL URLWithString:urlString]; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error) { NSLog(@"Failed to download framework: %@", error); return; } // Move the downloaded file to APP Contents/Frameworks folder NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *frameworksDirectory = [[NSBundle mainBundle] privateFrameworksPath]; NSString *destinationPath = [frameworksDirectory stringByAppendingPathComponent:@"DateTools.framework.zip"]; NSError *moveError = nil; [fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:destinationPath] error:&moveError]; if (moveError) { NSLog(@"Failed to move framework: %@", moveError); return; } NSLog(@"Framework downloaded and installed successfully!"); // 解压 framework 文件 [self unzipFrameworkAtURL:[NSURL fileURLWithPath:destinationPath] toDestination:frameworksDirectory]; // 加载 framework 文件 [self loadFramework:[frameworksDirectory stringByAppendingPathComponent:@"DateTools.framework"]]; }]; [downloadTask resume]; } // 解压 framework 文件 - (void)unzipFrameworkAtURL:(NSURL *)zipURL toDestination:(NSString *)destinationPath { // 解压逻辑,使用 Foundation 框架的 NSFileManager 或其他解压库 NSFileManager *fileManager = [NSFileManager defaultManager]; // 根据压缩文件类型选择解压方法 BOOL success = NO; if ([zipURL.path.pathExtension isEqualToString:@"zip"]) { // 使用系统自带的解压方法,需要引入 libz.tbd 库 success = [SSZipArchive unzipFileAtPath:zipURL.path toDestination:destinationPath]; } else { // 如果是其他类型的压缩文件,可以使用第三方库,如 SSZipArchive 等进行解压 // success = [SSZipArchive unzipFileAtPath:tempFilePath toDestination:destinationPath]; } if (success) { NSLog(@"File unzipped successfully!"); [fileManager removeItemAtPath:zipURL.path error:nil]; } else { NSLog(@"Failed to unzip file."); } } // 加载 framework 文件 - (void)loadFramework:(NSString *)destinationPath { NSError *error = nil; // NSURL *frameworkURL = [NSURL URLWithString:destinationPath]; // 指向解压后的 framework 文件目录 // NSString *frameworkPath = [[NSBundle mainBundle] pathForResource:@"DateTools" ofType:@"framework"]; NSBundle *frameworkBundle = [NSBundle bundleWithPath:destinationPath]; if (![frameworkBundle loadAndReturnError:&error]) { NSLog(@"Error loading framework: %@", error); return; } // 使用 framework 中的代码和资源 #if HAS_DOCUMENTAI_FRAMEWORK NSDate *selectedDate = [NSDate dateWithTimeIntervalSinceNow:-24*60*60*6+100]; NSString *week = [NSDate weekTimeAgoSinceDate:selectedDate]; NSLog(@"week == %@", week); #else // 其他处理或者警告用户没有导入 SomeFramework NSLog(@"没有库"); #endif } @end