前提是已經(jīng)配置好node\npm\cocoapos等環(huán)境
1、為React Native項(xiàng)目創(chuàng)建一個(gè)新的文件夾(比如我的roomrndemo),在文件夾中新建/ios目錄,拷貝所有原項(xiàng)目的內(nèi)容到/ios目錄下。
2、在剛剛新建的文件夾中(即根目錄中)創(chuàng)建一個(gè)package.json文件:
cd到根目錄:cd /Users/zhouyy/Desktop/roomrndemo ,執(zhí)行命令:npm init -y
此時(shí)會(huì)在根目錄下產(chǎn)生一個(gè)package.json內(nèi)容為:
{
? "name": "roomrndemo",
? "version": "1.0.0",
? "description": "",
? "main": "index.js",
? "scripts": {
? ? "test": "echo \"Error: no test specified\" && exit 1"
? },
? "keywords": [],
? "author": "",
? "license": "ISC",
}
下一步安裝react、react-native:
(關(guān)于package.json配置及用法參照這里:https://www.cnblogs.com/datiangou/p/10172994.html)
3、安裝最新react和react-native,
還是cd轉(zhuǎn)到當(dāng)前項(xiàng)目目錄下,
用命令:
npm install react --save? ? ,如果是安裝指定版本就用:npm install react@16.8.6 --save
install react-native --save? ,如果是安裝指定版本就用:npm install react-native@0.60.1 --save
注意react 和react-native版本對(duì)應(yīng)上,不過(guò)對(duì)應(yīng)不上,也會(huì)提示你安裝對(duì)應(yīng)版本的react 的,不加版本的話會(huì)默認(rèn)安裝最新版本,我就裝的最新版,下面會(huì)說(shuō)明解決最新版的問(wèn)題。。。
安裝完會(huì)增加node_modules、package-lock.json文件,其中package.json文件會(huì)發(fā)生變化:
{
? "name": "roomrndemo",
? "version": "1.0.0",
? "description": "",
? "main": "index.js",
? "scripts": {
? ? "test": "echo \"Error: no test specified\" && exit 1"
? },
? "keywords": [],
? "author": "",
? "license": "ISC",
? "dependencies": {
? ? "react": "^16.13.1",
? ? "react-native": "^0.62.2"
? }
}
執(zhí)行完上面命令后會(huì)發(fā)現(xiàn)自動(dòng)將package.json中的模塊安裝到node-modules文件夾下。node-modules里一堆的文件!
下一步就可以pod方式把node-modules中的本地文件引入原生工程了,當(dāng)然你不pod自己手動(dòng)拷進(jìn)去也是可以的,但是pod 引用的好處就是RN中發(fā)生改變,原生引用的也會(huì)自動(dòng)更新,這才是更官方的操作。
4、pod 引用(podfile配置是容易出問(wèn)題的,導(dǎo)致pod失敗,需要多檢查)
在原生工程中創(chuàng)建podfile文件:cd到工程,使用命令:pod init 會(huì)生成一個(gè)podfile文件
然后把要引用的RN文件庫(kù)添加到podfile中,使用引用本地庫(kù)的描述方式:
編輯podfile時(shí)注意!注意!:
(1)給 dynamic frameworks 添加cocoapods 的話,需要注意:
A、用CocoaPods 導(dǎo)入swift 框架 到 swift項(xiàng)目和OC項(xiàng)目都必須要 use_frameworks!
B、使用 dynamic frameworks,必須要在Podfile文件中添加 一句:use_frameworks!,如果是通過(guò)pod init指令生成的podfile文件,會(huì)自動(dòng)加上這個(gè),如果是從其他工程拷進(jìn)來(lái),可能沒(méi)有這句,注意加上!
選擇 PROJECR/TARGET -> Build Settings -> Allow non-modular includes in Framework Modules -> YES
注意: Project 和 Target 需要同時(shí)設(shè)置。
因?yàn)樾掳姹镜?Pods 的方式是將第三方庫(kù)制作成Dynamic Frameworks,相當(dāng)于在 MyFramework 中引用別的 Framework,需要告訴編譯器允許這種行為。
(2)pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'? //react-native 0.43.0以上必須添加這個(gè)!
注意:新版(我用的最新版RN:0.62.2版)的node_modules的庫(kù)文件的路徑發(fā)生了改變,如果是使用老版本的RN下的podfile路徑會(huì)報(bào)錯(cuò)找不到的,反正記得每次升級(jí)都得修改和檢查一遍podfile,重新install 一遍pod!
坑:新版本卻少React-DevSupport.podspec和React-RCTWebSocket.podspec文件,去老版本拷貝一份。新版缺少fishhook文件,也是從其他版本拷貝過(guò)來(lái)的。而且有的文件路徑發(fā)生了變化,也得檢查修改。
解決問(wèn)題:https://stackoverflow.com/questions/60300493/no-podspec-found-for-react-core-in-node-modules-react-native
之后還會(huì)有 一堆報(bào)錯(cuò),檢查,文件路徑不對(duì)的改過(guò)來(lái),文件缺少的去其他版本拷貝過(guò)來(lái),一步步解決報(bào)錯(cuò)。最后終于pod install成功了。所以謹(jǐn)慎升級(jí)RN的版本吧,覺(jué)得還是挺麻煩的。
ps:在ios目錄下pod install時(shí),總是會(huì)卡在boost-for-react-native,因?yàn)閲?guó)內(nèi)用戶拉GitHub的代碼會(huì)卡住,一直失敗,解決辦法:在ios目錄下Podfile文件中,加入以下代碼:
pod 'boost-for-react-native', :git => 'https://gitee.com/damon-s/boost-for-react-native.git’
(參考鏈接:https://blog.csdn.net/hxl517116279/article/details/104557850)
再pod install就可以下載下來(lái)了。
最后貼出來(lái)成功的podFile文件完整內(nèi)容是:
# Uncomment the next line to define a global platform for your project
# platform :ios, '10.0'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
target 'LiveVieoDemo' do
? # Comment the next line if you don't want to use dynamic frameworks
? use_frameworks!
? # Pods for LiveVieoDemo
pod 'boost-for-react-native', :git => 'https://gitee.com/damon-s/boost-for-react-native.git’
? pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
? pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
? pod 'RCTRequired', :path => "../node_modules/react-native/Libraries/RCTRequired"
? pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
? pod 'React', :path => '../node_modules/react-native/'
? pod 'React-Core', :path => '../node_modules/react-native/'
? pod 'React-DevSupport', :path => '../node_modules/react-native/React'
? pod 'React-fishhook', :path => '../node_modules/react-native/Libraries/fishhook'
? pod 'RCTTypeSafety', :path => '../node_modules/react-native/Libraries/TypeSafety'
? 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-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 'ReactCommon/callinvoker', :path => "../node_modules/react-native/ReactCommon"
? pod 'ReactCommon/turbomodule/core', :path => "../node_modules/react-native/ReactCommon"
? pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/yoga'? #RN>0.43.0版本以上得加這個(gè)
? 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'
? target 'LiveVieoDemoTests' do
? ? inherit! :search_paths
? ? # Pods for testing
? end
? target 'LiveVieoDemoUITests' do
? ? # Pods for testing
? end
end