React Native开发React Native开发经验集

0.60+ 在iOS原生项目中集成react-native

2019-11-12  本文已影响0人  王家薪

一切按照 React Native 中文网 操作 下面说下不同点

因为使用的0.60+的 react-native 所以需要在 Podfile 文件中增加
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

use_native_modules!
就像下面这样

platform :ios, '9.0'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
source 'https://github.com/CocoaPods/Specs.git'
target 'You target name' do

  # React-native
   pod 'React', :path => '../node_modules/react-native/'
   pod 'React-Core', :path => '../node_modules/react-native/React'
   pod 'React-DevSupport', :path => '../node_modules/react-native/React'
   pod 'React-fishhook', :path => '../node_modules/react-native/Libraries/fishhook'
   pod 'React-RCTActionSheet', :path => '../node_modules/react-native/Libraries/ActionSheetIOS'
   pod 'React-RCTAnimation', :path => '../node_modules/react-native/Libraries/NativeAnimation'
   pod 'React-RCTBlob', :path => '../node_modules/react-native/Libraries/Blob'
   pod 'React-RCTImage', :path => '../node_modules/react-native/Libraries/Image'
   pod 'React-ART', :path => '../node_modules/react-native/Libraries/ART'
   pod 'React-RCTLinking', :path => '../node_modules/react-native/Libraries/LinkingIOS'
   pod 'React-RCTNetwork', :path => '../node_modules/react-native/Libraries/Network'
   pod 'React-RCTSettings', :path => '../node_modules/react-native/Libraries/Settings'
   pod 'React-RCTText', :path => '../node_modules/react-native/Libraries/Text'
   pod 'React-RCTVibration', :path => '../node_modules/react-native/Libraries/Vibration'
   pod 'React-RCTWebSocket', :path => '../node_modules/react-native/Libraries/WebSocket'
 
   pod 'React-cxxreact', :path => '../node_modules/react-native/ReactCommon/cxxreact'
   pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
   pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
   pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
   pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
 
   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'
   use_native_modules!
end

接下来 pod install 可能出现 libwebp 无法下载的问题 可以参照

pod 'libwebp'失败的解决办法

如果 cocoapods 版本在 1.8+ 需要在 Podfile中添加 source 'https://github.com/CocoaPods/Specs.git'(可以参照上面的 Podfile 设置) 在指定使用本地 repo

一切安装好以后, 使用 xcode 打开 iOS 项目, 依次点击 Project->Target->Build Phases 点击左上角的加号选择 New Run Script Phase 添加两个脚本

image.png
Start Pacjager
export RCT_METRO_PORT="${RCT_METRO_PORT:=8081}"
echo "export RCT_METRO_PORT=${RCT_METRO_PORT}" > "${SRCROOT}/../node_modules/react-native/scripts/.packager.env"
if [ -z "${RCT_NO_LAUNCH_PACKAGER+xxx}" ] ; then
  if nc -w 5 -z localhost ${RCT_METRO_PORT} ; then
    if ! curl -s "http://localhost:${RCT_METRO_PORT}/status" | grep -q "packager-status:running" ; then
      echo "Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly"
      exit 2
    fi
  else
    open "$SRCROOT/../node_modules/react-native/scripts/launchPackager.command" || echo "Can't start packager automatically"
  fi
fi

Bundle React Native code and images

export NODE_BINARY=node
./node_modules/react-native/scripts/react-native-xcode.sh

Start Pacjager 用于 debug 自动运行服务
Bundle React Native code and images 用于打包自动生成 bundle

完成!

上一篇下一篇

猜你喜欢

热点阅读