1.项目导入cocoapods
2.在RN中文网下载最新RN包
3.将其工程中的node_modules复制到你的工程根目录下
4.在podfile 中加入以下带代码:
pod 'React', :path => './node_modules/react-native', :subspecs => [
'Core',
'RCTImage',
'RCTNetwork',
'RCTText',
'RCTWebSocket',
]
5.使用终端进入工程根目录pod update —no-repo-update
6.修改plist文件 app transport security http协议(访问js文件必须的)
App Transport Security Settings - Exception Domains-localhost-NSTemporaryExceptionAllowsInsecureHTTPLoads设置为yes
7.创建js文件放入根目录下的ReactComponent文件夹(可自定义,如需修改请同时修改第八条)
7.1 build setting-search paths -header search paths添加 "${PODS_ROOT}/Headers/Public” "${PODS_ROOT}/Headers/Public/React"
(改成recursive)
8.终端修改JS读取路径并开启服务 (首先cd 到项目文件夹下)
(JS_DIR= `pwd` /ReactComponent; cd node_modules/react-native; npm run start -- --root $JS_DIR)
注:ReactComponent为读取index.ios.js文件的路径
备注:每次运行均为手动打开服务器
9.运行Xcode上的程序
备注 :测试使用
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/ReactComponent/index.ios.bundle?platform=ios&dev=true"];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"RN"
initialProperties:nil
launchOptions:launchOptions];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];