如何使用CocoaPods在OC原生工程基礎(chǔ)上添加RN
1.創(chuàng)建原生工程
2.在工程根目錄使用pod init 安裝Cocoapods
3.在RN中文網(wǎng)下載最新RN包
將其工程中的node_modules復(fù)制到你的工程根目錄下
4.在podfile 中加入以下帶代碼:
pod 'React', :path => './node_modules/react-native', :subspecs => [ 'Core', 'RCTImage', 'RCTNetwork', 'RCTText', 'RCTWebSocket', //添加其他你想在工程中使用的依賴 ]
5.使用終端進入工程根目錄pod update
6.修改plist文件 app transport security http協(xié)議
7.創(chuàng)建視圖RCTRootView
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios”];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName: @“SimpleApp" launchOptions:nil];
[self addSubview:rootView];
rootView.frame = self.bounds;
8.終端修改JS讀取路徑并開啟服務(wù)
(JS_DIR= `pwd` /ReactComponent; cd node_modules/react-native; npm run start -- --root $JS_DIR)
注:ReactComponent為讀取index.ios.js文件的路徑
9.運行Xcode上的程序
以上內(nèi)容在RN中文網(wǎng)上教程基礎(chǔ)上有所添加。