https://reactnative.cn/docs/integration-with-existing-apps/
1、创建一个xcode工程,single View就行,项目语言选择swift,oc的直接生成就行不用这么麻烦。
2、把跟目录上创建 node的package.json,执行命令
npm init
npm install react-native --save-dev
npm install react --save-dev
3、创建podfile文件,然后执行pod install ,必须第二步完成。代码如下:
# Uncomment the next line to define a global platform for your project # platform :ios, '9.0'target 'demo1' do# Comment the next line if you're not using Swift and don't want to use dynamic frameworksuse_frameworks!pod 'DoubleConversion', :podspec => './node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'pod 'glog', :podspec => './node_modules/react-native/third-party-podspecs/GLog.podspec'pod 'Folly', :podspec => './node_modules/react-native/third-party-podspecs/Folly.podspec'pod 'React', :path => './node_modules/react-native', :subspecs => ['Core','DevSupport','RCTText','RCTNetwork','RCTWebSocket','CxxBridge']# 如果你的RN版本 >= 0.42.0,请加入下面这行pod "yoga", :path => "./node_modules/react-native/ReactCommon/yoga"end
执行npm start 启动服务
运行项目
出现如下错误
查了下资料 需要修改
在info.plist中添加 AppTransport Security Settings 在其下再添加 Allow Arbitrary Loads选择为 yes,修改网络可以使用http请求。
运行正常
ok, 新建Swift工程,配合Cocoapods 构建react-native项目就完成了。
demo地址
https://github.com/youhui/react-native-cocoapods-swift-project