美文网首页
iOS原有项目中配置ReactNative

iOS原有项目中配置ReactNative

作者: MTSu1e丶 | 来源:发表于2018-05-24 16:05 被阅读73次
  1. 打开终端输入cd 到项目的根目录,比如ReactNativeTest

  2. 创建package.json文件 :

$ npm init

然后一直回车键,就会创建成功

  1. 在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代替npmYarn是 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

  1. 完成安装react和react-natvie到node_modules 目录中:

$ npm install

安装完yarn之后可以使用

$ yarn
代替npm install

  1. 创建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
  1. 配置所有的依赖:

$ pod install

  1. 创建js文件:

$ touch index.ios.js

  1. 注意info.plist文件的修改:
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
  1. 启动ReactNative 的Packager服务器,然后就可以在xcode里面运行项目了:

$ npm start

  1. 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,但是会有很多问题,最典型的也是最不容易解决的就是如图:

10.png
解决办法是:
../node_modules/react-native/ReactCommon/yoga里面的yoga.podspec里面的最后一行添加:
spec.public_header_files = 'yoga/Yoga.h', 'yoga/YGEnums.h', 'yoga/YGMacros.h'

然后到profile的路径下执行

pod install

然后会出现


11.jpg 12.png

就是把这行代码改成

#import "fishhook.h"
#import "RCTValueAnimatedNode.h"

然后就可以成功运行了~

参考网址:
http://reactnative.cn/docs/0.48/integration-with-existing-apps.html#content

相关文章

网友评论

      本文标题:iOS原有项目中配置ReactNative

      本文链接:https://www.haomeiwen.com/subject/prusjftx.html