react native app離線打包準備

Android

新增main.jsbundle文件

首先在瀏覽器打開http://localhost:8081/index.ios.bundle?platform=android&dev=false(根據自己項目及平臺來),然後將自己創建一個文件main.jsbundle來保存此網址返回的代碼,最後放到Android的assets目錄中

新增寫文件代碼

 private static final String JSBUNDLE_FILE = "main.jsbundle";

  private static void copyFile(InputStream in, OutputStream out) throws IOException
  {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
  }

  private void prepareJSBundle() {
    File targetFile = new File(getFilesDir(), JSBUNDLE_FILE);
    if (!targetFile.exists()) {
      try {
        OutputStream out = new FileOutputStream(targetFile);
        InputStream in = getAssets().open(JSBUNDLE_FILE);

        copyFile(in, out);
        out.close();
        in.close();
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

onCreate中setContentView之後調用prepareJSBundle()即可;

Ios

新增main.jsbundle文件

首先在瀏覽器打開http://localhost:8081/index.ios.bundle?platform=ios&dev=false(根據自己項目及平臺來),然後將自己創建一個文件main.jsbundle來保存此網址返回的代碼,最後放到Ios的項目根目錄中

修改項目中加載代碼的判斷

註釋 jsCodeLocation = [NSURL URLWithString:@”http://localhost:8090/index.ios.bundle?platform=ios&dev=true“];

打開 jsCodeLocation = [[NSBundle mainBundle] URLForResource:@”main” withExtension:@”jsbundle”];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章