記錄 iOS原生工程嵌入react native 及蹚坑

前提是已經(jīng)配置好node\npm\cocoapos等環(huán)境

1、為React Native項目創(chuàng)建一個新的文件夾(比如我的roomrndemo),在文件夾中新建/ios目錄,拷貝所有原項目的內(nèi)容到/ios目錄下。

2、在剛剛新建的文件夾中(即根目錄中)創(chuàng)建一個package.json文件:

cd到根目錄:cd /Users/zhouyy/Desktop/roomrndemo ,執(zhí)行命令:npm init -y

此時會在根目錄下產(chǎn)生一個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:

(關于package.json配置及用法參照這里:https://www.cnblogs.com/datiangou/p/10172994.html)

3、安裝最新react和react-native,

還是cd轉(zhuǎn)到當前項目目錄下,

用命令:

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版本對應上,不過對應不上,也會提示你安裝對應版本的react 的,不加版本的話會默認安裝最新版本,我就裝的最新版,下面會說明解決最新版的問題。。。

安裝完會增加node_modules、package-lock.json文件,其中package.json文件會發(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í)行完上面命令后會發(fā)現(xiàn)自動將package.json中的模塊安裝到node-modules文件夾下。node-modules里一堆的文件!

下一步就可以pod方式把node-modules中的本地文件引入原生工程了,當然你不pod自己手動拷進去也是可以的,但是pod 引用的好處就是RN中發(fā)生改變,原生引用的也會自動更新,這才是更官方的操作。

4、pod 引用(podfile配置是容易出問題的,導致pod失敗,需要多檢查)

在原生工程中創(chuàng)建podfile文件:cd到工程,使用命令:pod init 會生成一個podfile文件


然后把要引用的RN文件庫添加到podfile中,使用引用本地庫的描述方式:

編輯podfile時注意!注意?。?/p>

(1)給 dynamic frameworks 添加cocoapods 的話,需要注意:

A、用CocoaPods 導入swift 框架 到 swift項目和OC項目都必須要 use_frameworks!

B、使用 dynamic frameworks,必須要在Podfile文件中添加 一句:use_frameworks!,如果是通過pod init指令生成的podfile文件,會自動加上這個,如果是從其他工程拷進來,可能沒有這句,注意加上!

選擇 PROJECR/TARGET -> Build Settings -> Allow non-modular includes in Framework Modules -> YES

注意: Project 和 Target 需要同時設置。

因為新版本的 Pods 的方式是將第三方庫制作成Dynamic Frameworks,相當于在 MyFramework 中引用別的 Framework,需要告訴編譯器允許這種行為。

(2)pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'? //react-native 0.43.0以上必須添加這個!

注意:新版(我用的最新版RN:0.62.2版)的node_modules的庫文件的路徑發(fā)生了改變,如果是使用老版本的RN下的podfile路徑會報錯找不到的,反正記得每次升級都得修改和檢查一遍podfile,重新install 一遍pod!

坑:新版本卻少React-DevSupport.podspec和React-RCTWebSocket.podspec文件,去老版本拷貝一份。新版缺少fishhook文件,也是從其他版本拷貝過來的。而且有的文件路徑發(fā)生了變化,也得檢查修改。

解決問題:https://stackoverflow.com/questions/60300493/no-podspec-found-for-react-core-in-node-modules-react-native

之后還會有 一堆報錯,檢查,文件路徑不對的改過來,文件缺少的去其他版本拷貝過來,一步步解決報錯。最后終于pod install成功了。所以謹慎升級RN的版本吧,覺得還是挺麻煩的。

ps:在ios目錄下pod install時,總是會卡在boost-for-react-native,因為國內(nèi)用戶拉GitHub的代碼會卡住,一直失敗,解決辦法:在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就可以下載下來了。


成功pod


關閉Xcode重新打開工程

最后貼出來成功的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版本以上得加這個

? 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

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容