-
打开终端输入cd 到项目的根目录,比如ReactNativeTest
-
创建package.json文件 :
$ npm init
然后一直回车键,就会创建成功
- 在package.json文件中写入:
{
"name": "ReactNativeTest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^16.0.0-alpha.12",
"react-native": "^0.54.0"
}
}
注意这里如果写 "react-native": "^0.56.0"
的话,那么podfile里面的platform必须的是ios 9
所以我这里写的是0.54.0
4.使用Facebook生产的yarn
代替npm
,Yarn是 Facebook 提供的替代 npm 的工具,可以加速 node 模块的下载。React Native 的命令行工具用于执行创建、初始化、更新项目、运行打包服务(packager)等任务。
npm install -g yarn react-native-cli
安装完yarn之后要设立镜像:
yarn config set registry https://registry.npm.taobao.org --global
yarn config set disturl https://npm.taobao.org/dist --global
- 完成安装react和react-natvie到node_modules 目录中:
$ npm install
安装完yarn
之后可以使用
$ yarn
代替npm install
- 创建Podfile文件:
$ pod init
在podfile文件里面写:
platform :ios, '8.0'
target “ReactNativeTest” do
pod ‘Yoga’, :path => ‘./node_modules/react-native/ReactCommon/yoga’
pod 'React', :path => ‘./node_modules/react-native', :subspecs => [
'Core',
'RCTImage',
'RCTNetwork',
'DevSupport',
'RCTText',
'RCTWebSocket',
'BatchedBridge’,
]
end
target ‘ReactNativeTestUITests’ do
end
- 配置所有的依赖:
$ pod install
- 创建js文件:
$ touch index.ios.js
- 注意info.plist文件的修改:
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
- 启动ReactNative 的Packager服务器,然后就可以在xcode里面运行项目了:
$ npm start
- xcode退出程序重新启动
注:
查看当前本地的ReactNative版本:
react-native --v
查看远程的最新的ReactNative版本:
npm info react-native
更新到最新版本:
npm install --save react-native@x.xx.x
更新react到最新版本:
yarn add react@x.xx.x
遇到的问题:
当前ReactNative的最新版本是0.56.0
1.如果你项目是ios 9.0
以上,也就是你项目podfile
里面 platform :ios, '9.0'
,你就可以用ReactNative 0.56.0版本,就不会遇到很多问题
2.如果你的项目支持ios 8.0
系统以上的时候,你就不可以用ReactNative的0.56.0版本了。可以用0.55.4
,但是会有很多问题,最典型的也是最不容易解决的就是如图:

解决办法是:
到
../node_modules/react-native/ReactCommon/yoga
里面的yoga.podspec
里面的最后一行添加:
spec.public_header_files = 'yoga/Yoga.h', 'yoga/YGEnums.h', 'yoga/YGMacros.h'
然后到profile
的路径下执行
pod install
然后会出现


就是把这行代码改成
#import "fishhook.h"
#import "RCTValueAnimatedNode.h"
然后就可以成功运行了~
参考网址:
http://reactnative.cn/docs/0.48/integration-with-existing-apps.html#content
网友评论