1. 在現(xiàn)有的項目(以 ReactNativeTest 為例)基礎(chǔ)上導入,ReactNativeTest 已經(jīng)集成了cocoapods (注:版本要在1.2.0以上,否則后面會報錯)
? ? 查看cocoapods版本命令??
? ? pod??- -version
2.安裝React Native的命令行工具
? ?npm install -g react-native-cli
3.進入你工程的根目錄
4.這個工作跟cocoapods的podfile文件初始化有點像 ?-> ?根據(jù)提示輸入工程的package.json的內(nèi)容。
? ?npm init?
5.編輯package.json,往dependencies添加如下依賴(以下是我編輯好的內(nèi)容)
? {
? ? ?"name": "react-one",
? ? ?"version": "1.0.0",
? ? "description": "first init",
? ? "main": "index.js",
? ?"dependencies": {
? ?"react": "15.4.1",
? ?"react-native": "^0.43.4"
?},?
? ?"devDependencies": {},
? ?"scripts": {
? "test": "first test"
},
? "repository": {
? "type": "git",
? "url": "main"
},
? "keywords": [
? "first",
? "test"
?],
? "author": "NB_killer",
? "license": "ISC"
?}
6.安裝
npm install
如果需要更新:
react-native upgrade
7.配置podfile
platform :ios, '9.0'
target 'ReactNativeTest' do
# 'node_modules'目錄一般位於根目錄中
# 但是如果你的結(jié)構(gòu)不同,那你就要根據(jù)實際路徑修改下面的`:path`
pod 'React', :path => './node_modules/react-native', :subspecs => [
'Core',
'RCTText',
'RCTNetwork',
'RCTWebSocket', # 這個模塊是用於調(diào)試功能的
# 在這裏繼續(xù)添加你所需要的模塊
]
# 如果你的RN版本 >= 0.42.0,請加入下面這行
pod 'Yoga', :path => './node_modules/react-native/ReactCommon/yoga’
end
8.導入類庫
pod install
9.編輯 index.ios.js
10.添加libraries
11.啟動開發(fā)服務(wù)器
? ? ?在文件根目錄執(zhí)行
? ? react-native start
12.設(shè)置網(wǎng)絡(luò)環(huán)境
13.在 RNViewController控制器中 導入 RN
14.運行ios項目
? ? ?通過Xcode點擊項目或者command + R運行項目,就會看到成功運行的界面:
15.總結(jié)
? ? 在實際導入過程中可能遇到一些錯誤,不要著急,有錯就去github 或者 Stack Overflow 上找答案,祝君好運!