熱修復(fù)
打包
"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];
//解壓目標(biāo)路徑
__block NSString *destinationPath =[documentPath stringByAppendingPathComponent:@"rn"];
if ([[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) { //一定要?jiǎng)h除老的
[[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"]];
}