// // JSBundleManager.m // san_huo_sheng_ri_ben // // Created by 朱吉峥 on 2024/2/26. // #import "JSBundleManager.h" //#import "AFNetworking.h" #import @implementation JSBundleManager // //+ (void)downloadFile //{ // /* 创建网络下载对象 */ // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; // /* 下载地址 */ // NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; // NSString *tmpPath = [documentsDir stringByAppendingPathComponent:@"temp.jsbundle"]; // NSString *path = [documentsDir stringByAppendingPathComponent:@"main.jsbundle"]; // NSURL *url =[NSURL URLWithString:@"https://gitee.com/zhujizheng/san_huo_sheng_ri_ben/raw/master/ios/bundle/main.jsbundle"]; // NSURLRequest *request = [NSURLRequest requestWithURL:url]; // [manager setDownloadTaskDidFinishDownloadingBlock:^NSURL * _Nullable(NSURLSession * _Nonnull session, NSURLSessionDownloadTask * _Nonnull downloadTask, NSURL * _Nonnull location) { // /* 设定下载到的位置 */ // return [NSURL fileURLWithPath:tmpPath]; // }]; // /* 开始请求下载 */ // NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { // // } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { // dispatch_async(dispatch_get_main_queue(), ^{ // //如果需要进行UI操作,需要获取主线程进行操作 // }); // /* 设定下载到的位置 */ // return [NSURL fileURLWithPath:tmpPath]; // // } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { // NSLog(@"JSBundleManager error:%@, path:%@", error, path); // NSFileManager *fileManager = [NSFileManager defaultManager]; // if (!error) { // [fileManager copyItemAtPath:tmpPath toPath:path error:nil]; // } // if ([fileManager fileExistsAtPath:tmpPath]) { // [fileManager removeItemAtPath:tmpPath error:nil]; // } // }]; // [downloadTask resume]; //} + (NSURL *)bundleURL { #if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"]; #else return [self releaseBundleURL]; #endif } + (NSURL *)releaseBundleURL { NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *path = [documentsDir stringByAppendingPathComponent:@"main.jsbundle"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:path]) { return [NSURL fileURLWithPath:path]; } return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; } @end