?
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