React Native發布APP之打包iOS應用

來源:互聯網
上載者:User

標籤:sso   div   roi   不同   out   這一   nbu   false   str   

第一步:匯出js bundle包和圖片資源

和打包React Native Android應用不同的是,我們無法通過命令一步進行匯出React Native iOS應用。我們需要將JS部分的代碼和圖片資源等打包匯出,然後通過XCode將其添加到iOS項目中。

匯出js bundle的命令

在React Native項目的根目錄下執行:

1 react-native bundle --entry-file index.ios.js --platform ios --dev false --bundle-output release_ios/main.jsbundle --assets-dest release_ios/

通過上述命令,我們可以將JS部分的代碼和圖片資源等打包匯出到release_ios目錄下:

產生jsbundle

其中,assets為項目中的JS部分所用到的圖片資源(不包括原生模組中的圖片資源),main.jsbundle是JS部分的代碼。

在執行打包命令之前,我們需要先確保在我們項目的根目錄有release_ios檔案夾,沒有的話建立一個。

第二步:將js bundle包和圖片資源匯入到iOS項目中

這一步我們需要用到XCode,選擇assets檔案夾與main.jsbundle檔案將其拖拽到XCode的項目導航面板中即可。

匯入jsbundle

然後,修改AppDelegate.m檔案,添加如下代碼:

12345678910 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{   NSURL *jsCodeLocation; //jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil]; +jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];#endif...  return YES;}

上述代碼的作用是讓React Native去使用我們剛才匯入的jsbundle,這樣以來我們就擺脫了對本地nodejs伺服器的依賴。

提示:如果在項目中使用了CodePush熱更新,那麼我們需要就可以直接通過CodePush來讀取本地的jsbundle,方法如下:

1234567891011 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{  NSURL *jsCodeLocation;  #ifdef DEBUG    jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];#else    jsCodeLocation = [CodePush bundleURL];#endif...  return YES;}

到目前為止呢,我們已經將js bundle包和圖片資源匯入到iOS項目中,接下來我們就發行就緒我們的iOS應用了。

第三步:發布iOS應用

。。。

 

原文連結

React Native發布APP之打包iOS應用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.