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

前提是已經(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)了。


成功pod


關(guān)閉Xcode重新打開(kā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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,505評(píng)論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,556評(píng)論 3 418
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 176,463評(píng)論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 63,009評(píng)論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,778評(píng)論 6 410
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 55,218評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,281評(píng)論 3 441
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 42,436評(píng)論 0 288
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,969評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,795評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,993評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,537評(píng)論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,229評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 34,659評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 35,917評(píng)論 1 286
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,687評(píng)論 3 392
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,990評(píng)論 2 374