iOS端實(shí)現(xiàn)React Native差異化增量更新

扯會(huì)淡

RN具有的優(yōu)勢(shì)有很多(雖然坑更多,一代版本一筐坑)葡盗,跨平臺(tái)開發(fā)尘盼,一套代碼Android和iOS通用偷办,熱更新统舀,不用一直等蘋果爸爸慢吞吞的審核流程匆骗,既然要做RN,那么RN的熱更新部署肯定得學(xué)下,今天就總結(jié)一下一個(gè)剛學(xué)RN的小白對(duì)熱更新的理解誉简。

個(gè)人理解碉就,RN的熱更新有點(diǎn)類似App的版本更新,app內(nèi)版本號(hào)與server端匹配闷串,來判斷是否要更新瓮钥,替換加載的jsbundle文件,然后加載新的jsbundle文件來實(shí)現(xiàn)版本更新,那么實(shí)質(zhì)上就是把a(bǔ)pp內(nèi)要加載的jsbundle文件替換掉就OK了碉熄。

原理分析

自己整理的原理圖.png

react-native打ios離線包

  • 打包命令說明
react-native bundle
Options:
--entry-file <path>          Path to the root JS file, either absolute or relative to JS root
(一般為index.js文件)
--platform [string]          Either "ios" or "android"
(RN入口文件的路徑, 絕對(duì)路徑或相對(duì)路徑)
--transformer [string]       Specify a custom transformer to be used

--dev [boolean]              If false, warnings are disabled and the bundle is minified
(如果為false, 警告會(huì)不顯示并且打出的包的大小會(huì)變小桨武,默認(rèn)為--dev true)
--prepack                    When passed, the output bundle will use the Prepack format.
(當(dāng)通過時(shí), 打包輸出將使用Prepack格式化,默認(rèn)為--prepack  false)
--bridge-config [string]     File name of a a JSON export of __fbBatchedBridgeConfig. Used by Prepack. Ex. ./bridgeconfig.json
(使用Prepack的一個(gè)json格式的文件__fbBatchedBridgeConfig 例如: ./bridgeconfig.json)
 --bundle-output <string>     File name where to store the resulting bundle, ex. /tmp/groups.bundle
(打包后的文件輸出目錄, 例: /tmp/groups.bundle)
--bundle-encoding [string]   Encoding the bundle should be written in (https://nodejs.org/api/buffer.html#buffer_buffer).[default: "utf8"]
(打離線包的格式 可參考鏈接https://nodejs.org/api/buffer.html#buffer_buffer.默認(rèn)為utf-8格式)
---sourcemap-output [string]  File name where to store the sourcemap file for resulting bundle, ex. /tmp/groups.map
(生成Source Map锈津,但0.14之后不再自動(dòng)生成source map呀酸,需要手動(dòng)指定這個(gè)參數(shù)。例: /tmp/groups.map)
--assets-dest [string]       Directory name where to store assets referenced in the bundle
(打包時(shí)圖片資源的存儲(chǔ)路徑)
--verbose                    Enables logging
(顯示打包過程)
--reset-cache                Removes cached files
(移除緩存文件)
--config [string]            Path to the CLI configuration file
(命令行的配置文件路徑)
  • 具體操作
    1.cd [項(xiàng)目路徑]
    2.在react-native根目錄下的ios目錄下新建bundle文件夾(mkdir ./ios/bundle)(注意:輸入打包命令前必須先新建bundle文件夾)
    3.打包命令:react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ./ios/bundle/index.ios.jsbundle --assets-dest ./ios/bundle/
    4.結(jié)果展示


    生成的jsbundle離線包

    一般為下面這種


    生成的jsbundle離線包

patches.pad差異化文件終端生成方案

利用google的diff文件(資料查出來琼梆,這個(gè)比較受歡迎性誉,同時(shí)也兼容Objective-C),github地址:https://github.com/google/dif...

  • $ git clone https://github.com/LiuC520/no...
  • $ cd nodediffpatch && npm i
  • $ sudo npm link
  • 把新舊文件放入nodediffpatch/patch目錄下


    新舊離線包
  • 終端輸入:patbundle patch -o test01old.jsbundle -n test01new.jsbundle


    生成的差異化文件

iOS實(shí)現(xiàn)生成差異化文件

簡(jiǎn)單方法:把diff-match-patch實(shí)現(xiàn)源碼拖進(jìn)工程中


選擇源碼
拖進(jìn)工程中

導(dǎo)入#import "DiffMatchPatch.h"開始使用,下面演示用l1.txt和l2.txt文件來展示茎杂,可以比較直觀的看出效果
l1.txt文本:123
l2.txt文本:12345

- (void)demo1{
  // 獲取l1.txt文件路徑
  NSString *path01 = [[NSBundle mainBundle]pathForResource:@"l1" ofType:@"txt"];
  // 根據(jù)l1.txt文件路徑獲取data內(nèi)容
  NSData *data01 = [NSData dataWithContentsOfFile:path01];
  // 將data內(nèi)容轉(zhuǎn)換成字符串格式
  NSString *str01 = [[NSString alloc] initWithData:data01 encoding:NSUTF8StringEncoding];
  // 獲取l2.txt文件路徑
  NSString *path02 = [[NSBundle mainBundle]pathForResource:@"l2" ofType:@"txt"];
  // 根據(jù)l2.txt文件路徑獲取data內(nèi)容
  NSData *data02 = [NSData dataWithContentsOfFile:path02];
  // 將data內(nèi)容轉(zhuǎn)換成字符串格式
  NSString *str02 = [[NSString alloc] initWithData:data02 encoding:NSUTF8StringEncoding];
  // 創(chuàng)建DiffMatchPatch工具類對(duì)象
  DiffMatchPatch *patch = [[DiffMatchPatch alloc]init];
  // 對(duì)比文件內(nèi)容
  // 執(zhí)行該語句之后會(huì)在bundle目錄下生成patches.bat文件(差異補(bǔ)丁文件)
  NSMutableArray *patchesArr = [patch diff_mainOfOldString:str01 andNewString:str02 checkLines:YES];
  // 生成差異補(bǔ)丁包
  NSArray *patchesArr1 = [patch patch_makeFromDiffs:patchesArr];
  // 解析補(bǔ)丁包
  NSArray *newArray = [patch patch_apply:patchesArr1 toString:str01];
  //寫入到新文件(注意:這邊為了在PC端更加直觀的看,直接寫入到絕對(duì)路徑)
  BOOL isTrue = [newArray[0] writeToFile:@"/Users/devil/Desktop/自己的/RNPlatForm/ios/l1.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
  if (isTrue) {
    NSLog(@"寫入成功");
  }else{
    NSLog(@"寫入失敗");
  }
}

執(zhí)行代碼后:
l1.txt文本:12345

iOS實(shí)現(xiàn)patches.pat與舊jsbundle離線包合并得到新的jsbundle離線包

- (void)demo2{
  // 獲取l1.txt文件路徑
  NSString *path01 = [[NSBundle mainBundle]pathForResource:@"l1" ofType:@"txt"];
  // 根據(jù)l1.txt文件路徑獲取data內(nèi)容
  NSData *data01 = [NSData dataWithContentsOfFile:path01];
  // 將data內(nèi)容轉(zhuǎn)換成字符串格式
  NSString *str01 = [[NSString alloc] initWithData:data01 encoding:NSUTF8StringEncoding];
  // 創(chuàng)建DiffMatchPatch工具類對(duì)象
  DiffMatchPatch *patch = [[DiffMatchPatch alloc]init];
  // 獲取差異化文件包路徑
  NSString *patchesPath = [[NSBundle mainBundle]pathForResource:@"patches.pat" ofType:nil];
  //獲取差異化文件內(nèi)容
  NSData *patchesData = [NSData dataWithContentsOfFile:patchesPath];
  //解析差異化文件內(nèi)容
  NSString *patchesStr = [[NSString alloc]initWithData:patchesData encoding:NSUTF8StringEncoding];
  //轉(zhuǎn)換pat
  NSMutableArray *patchesArr = [patch patch_fromText:patchesStr error:nil];
  // 解析補(bǔ)丁包
  NSArray *newArray = [patch patch_apply:patchesArr toString:str01];
  //獲取新文件路徑
//  NSString *newFilePath = [[NSBundle mainBundle]pathForResource:@"text3" ofType:@"txt"];
  //寫入到新文件(注意:這邊為了在PC端更加直觀的看,直接寫入到絕對(duì)路徑)
  BOOL isTrue = [newArray[0] writeToFile:@"/Users/devil/Desktop/自己的/RNPlatForm/ios/text3.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
  if (isTrue) {
    NSLog(@"寫入成功");
  }else{
    NSLog(@"寫入失敗");
  }
}

實(shí)現(xiàn)本地更新離線包

//創(chuàng)建兩個(gè)按鈕错览,第一個(gè)按鈕跳轉(zhuǎn)RN界面,加載jsbundle包煌往,第二個(gè)按鈕負(fù)責(zé)更新jsbundle包
UIButton *btn4 = [[UIButton alloc]init];
  [btn4 setTitle:@"第五個(gè)" forState:UIControlStateNormal];
  [btn4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  btn4.frame = CGRectMake(40, 170, 60, 30);
  [btn4 addTarget:self action:@selector(clickFifth) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:btn4];
  
  UIButton *btn5 = [[UIButton alloc]init];
  [btn5 setTitle:@"更新第五個(gè)界面" forState:UIControlStateNormal];
  [btn5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  btn5.frame = CGRectMake(40, 200, 60, 30);
  [btn5 addTarget:self action:@selector(demo2) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:btn5];
//btn4按鈕點(diǎn)擊事件
- (void)clickFifth{
  NSURL *jsCodeLocation;
  jsCodeLocation = [[NSBundle mainBundle]URLForResource:@"test01old" withExtension:@"jsbundle"];
  [self creactRNPath:jsCodeLocation moduleName:@"test01platcode"]; 
}

- (void)creactRNPath:(NSURL *)jsCodeLocation moduleName:(NSString *)moduleName{
  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:moduleName
                                               initialProperties:nil
                                                   launchOptions:nil];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
  UIViewController *rootViewController = [[UIViewController alloc]init];
  rootViewController.view = rootView;
  [rootViewController.navigationController setNavigationBarHidden:YES animated:YES];
  [self.navigationController pushViewController:rootViewController animated:YES];
}
//btn5按鈕點(diǎn)擊事件
- (void)demo2{
  // 獲取test01old.jsbundle文件路徑
  NSString *path01 = [[NSBundle mainBundle]pathForResource:@"test01old" ofType:@"jsbundle"];
  // 根據(jù)test01old.jsbundle文件路徑獲取data內(nèi)容
  NSData *data01 = [NSData dataWithContentsOfFile:path01];
  // 將data內(nèi)容轉(zhuǎn)換成字符串格式
  NSString *str01 = [[NSString alloc] initWithData:data01 encoding:NSUTF8StringEncoding];
  // 創(chuàng)建DiffMatchPatch工具類對(duì)象
  DiffMatchPatch *patch = [[DiffMatchPatch alloc]init];
  // 獲取差異化文件包路徑
  NSString *patchesPath = [[NSBundle mainBundle]pathForResource:@"test01patches.pat" ofType:nil];
  //獲取差異化文件內(nèi)容
  NSData *patchesData = [NSData dataWithContentsOfFile:patchesPath];
  //解析差異化文件內(nèi)容
  NSString *patchesStr = [[NSString alloc]initWithData:patchesData encoding:NSUTF8StringEncoding];
  //轉(zhuǎn)換pat
  NSMutableArray *patchesArr = [patch patch_fromText:patchesStr error:nil];
  // 解析補(bǔ)丁包
  NSArray *newArray = [patch patch_apply:patchesArr toString:str01];
  //寫入到新文件
  BOOL isTrue = [newArray[0] writeToFile:path01 atomically:YES encoding:NSUTF8StringEncoding error:nil];
  if (isTrue) {
    NSLog(@"寫入成功");
  }else{
    NSLog(@"寫入失敗");
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蝗砾,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子携冤,更是在濱河造成了極大的恐慌,老刑警劉巖闲勺,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件曾棕,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡菜循,警方通過查閱死者的電腦和手機(jī)翘地,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來癌幕,“玉大人衙耕,你說我怎么就攤上這事∩自叮” “怎么了橙喘?”我有些...
    開封第一講書人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)胶逢。 經(jīng)常有香客問我厅瞎,道長(zhǎng),這世上最難降的妖魔是什么初坠? 我笑而不...
    開封第一講書人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任和簸,我火速辦了婚禮,結(jié)果婚禮上碟刺,老公的妹妹穿的比我還像新娘锁保。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開白布爽柒。 她就那樣靜靜地躺著吴菠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪霉赡。 梳的紋絲不亂的頭發(fā)上橄务,一...
    開封第一講書人閱讀 52,682評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音穴亏,去河邊找鬼蜂挪。 笑死,一個(gè)胖子當(dāng)著我的面吹牛嗓化,可吹牛的內(nèi)容都是我干的棠涮。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼刺覆,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼严肪!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起谦屑,我...
    開封第一講書人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤驳糯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后氢橙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體酝枢,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年悍手,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了帘睦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡坦康,死狀恐怖竣付,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情滞欠,我是刑警寧澤古胆,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站筛璧,受9級(jí)特大地震影響赤兴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜隧哮,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一桶良、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧沮翔,春花似錦陨帆、人聲如沸曲秉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽承二。三九已至,卻和暖如春纲爸,著一層夾襖步出監(jiān)牢的瞬間亥鸠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工识啦, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留负蚊,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓颓哮,卻偏偏與公主長(zhǎng)得像家妆,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子冕茅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容