美文网首页
RN热修复【iOS】

RN热修复【iOS】

作者: sujeking | 来源:发表于2020-10-30 13:36 被阅读0次

热修复

打包

"pack-app": "react-native bundle --platform ios --entry-file index.js --bundle-output ./bundles/main.jsbundle --assets-dest ./bundles --dev false",

下载和加载

# pod 'SSZipArchive'

############################################
下载
############################################
- (void)netDownloadSourceFile {
    __weak typeof(self) weakSelf = self;
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSString *urlstr = @"http://127.0.0.1:9999/app.zip"; //下载地址
    NSURL *url = [NSURL URLWithString:urlstr];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSString *path=[NSHomeDirectory() stringByAppendingString:@"/Documents/szw"];
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    NSString *filePath = [path stringByAppendingPathComponent:url.lastPathComponent];
    
    NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:req progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%.2f",downloadProgress.fractionCompleted * 100);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        return [NSURL fileURLWithPath:filePath];
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        NSLog(@"下载完成");
        
        [weakSelf pageSetupUI];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@",filePath);
        });
    }];
    [task resume];
}
############################################
解压
############################################
- (void)unzipFileWithPath:(NSString *)filePath {
    //Document路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    //解压目标路径
    __block NSString *destinationPath =[documentPath stringByAppendingPathComponent:@"rn"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) { //一定要删除老的
        [[NSFileManager defaultManager] removeItemAtPath:destinationPath error:nil];
    }
    //解压
    __weak typeof(self) weakSelf = self;
    [SSZipArchive unzipFileAtPath:filePath toDestination:destinationPath progressHandler:^(NSString * _Nonnull entry, unz_file_info zipInfo, long entryNumber, long total) {
        
    } completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nullable error) {
        weakSelf.unzipPath = destinationPath;
        [weakSelf pageSetupUI];
    }];   
}

############################################
加载RN
############################################
- (void)pageSetupUI {
    RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:@{}]; //RCTBridgeDelegate
    RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                     moduleName:@"app"
                                              initialProperties:nil];
    rootView.backgroundColor = [UIColor whiteColor];
    self.view = rootView;
}

// MARK: - RCTBridgeDelegate
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
    SendEventManager *sendEventManager = [[SendEventManager alloc] init];
    sendEventManager.bridge = bridge;
    return [NSURL fileURLWithPath:[self.unzipPath stringByAppendingPathComponent:@"main.jsbundle"]];
}

相关文章

网友评论

      本文标题:RN热修复【iOS】

      本文链接:https://www.haomeiwen.com/subject/nlhdvktx.html