美文网首页ReactNative收集
使用Cocoapods集成react native

使用Cocoapods集成react native

作者: 咸鱼永动机 | 来源:发表于2016-09-16 09:13 被阅读3932次

    对于react native(一下简称RCT)很多人应该都听说过,但真的集成使用,还是没有过的,近期在为已有项目集成RCT遇上了很多坑,发现网上挺多资料,但是看着乱的很,于是自己整理了一下:

    在集成之前,你的电脑必须安装:

    step 1.在项目中加入Cocoapods

    Cocoapods是iOS中目前公认的最便捷,合理的第三方框架管理工具,具体的网上很多相关的文章,一搜就可以了。

    项目工程根目录下
    pod init
    pod install
    

    step 2.安装react-native

    先创建一个文件夹用来存放react-native

    mkdir react
    

    进入创建的react文件中

    cd react 
    

    创建package.json
    文件 记得删除注释

    {
      "name": "你的项目名",
      "version": "0.0.1",
      "private": true,
      "scripts": {
        "start": "node node_modules/react-native/local-cli/cli.js start"
      },
      "dependencies": {  // 这里填入依赖项
        "react": "16.0.0",  // 其实 react也是作为一个第三方库存在的
        "react-native": "0.50"  // 这个也是 - - 依次可以加入很多第三方库,然后使用npm install初始化,其实这里和cocoapods很相似
      }
    }
    

    在react文件夹中安装react-native

    npm install
    

    创建index.ios.js文件

     /**
     * Sample React Native App
     * https://github.com/facebook/react-native
     * @flow
     */
    
    import React, { Component } from 'react';
    import {
      AppRegistry,
      StyleSheet,
      Text,
      View
    } from 'react-native';
    
    export default class ModuleA extends Component {
      render() {
        return (
          <View style={styles.container}>
            <Text style={styles.welcome}>
              Welcome to React Native!
            </Text>
            <Text style={styles.instructions}>
              To get started, edit index.ios.js
            </Text>
            <Text style={styles.instructions}>
              Press Cmd+R to reload,{'\n'}
              Cmd+D or shake for dev menu
            </Text>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
      },
      welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
      },
      instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
      },
    });
    
    AppRegistry.registerComponent('ModuleA', () => ModuleA);
    

    step 3.使用Cocoapods集成react-native

    Podfile中添加React

    pod 'React', :path => './react/node_modules/react-native', :subspecs => [
      'Core',
      'DevSupport',
      'RCTText',
      'RCTNetwork',
      'RCTWebSocket',
      'BatchedBridge',
      ]
      pod 'yoga', :path => './react/node_modules/react-native/ReactCommon/yoga'
    # 根据需求添加项目中需要的模块
    

    注意:./react/node_modules/react-native这个react是和你开始创建的存放RCT的目录是相同的。记得要添加所有你需要的依赖!举例来说, 元素如果不添加RCTText依赖就不能运行。
    更新pod

    pod update --no-repo-update
    

    到这里,配置工作就基本完成了,接下来跑起来试一下

    step 4.run run run

    先进入开始安装RCT的目录中,开启服务

    react-native start
    

    在iOS 9以上的系统中,除非明确指明,否则应用无法通过http协议连接到localhost主机。 我们建议你在Info.plist文件中将localhost列为App Transport Security的例外。 如果不这样做,在尝试通过http连接到服务器时,会遭遇这个错误 - Could not connect to development server.
    打开工程中的 Info.list 文件,添加下面配置即可:

    <key>NSAppTransportSecurity</key> 
    <dict> 
      <key>NSExceptionDomains</key> 
      <dict> 
        <key>localhost</key> 
        <dict>   
          <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> 
          <true/> 
        </dict> 
      </dict>
    </dict> 
    

    做完配置,就可以开始写代码了

    NSString * strUrl = @"http://localhost:8081/index.ios.bundle?platform=ios&dev=true";
    NSURL * jsCodeLocation = [NSURL URLWithString:strUrl]; 
    RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"ModuleA" initialProperties:nil launchOptions:nil]; 
    [self.view addSubview:rootView];rootView.frame = CGRectMake(0, 20, 300, 300);
    

    相关文章

      网友评论

      • 鹏雨燕:如果我想加一个额外的subspec,如react-navigation,在cocoapods该怎么写
      • 花果山松鼠:问题已经完美解决,谢谢作者。如果大家有疑问也可以咨询我,很乐意帮助大家。
        花果山松鼠:@盛夏已知深秋 先确保关于RN的那些软件都安装上了。请查看RN中文网,https://reactnative.cn/docs/0.51/getting-started.html,然后一步一步检查吧,主要还是配置和路径的问题。
        盛夏已知深秋:手动集成高德地图SDK时候会出现类似你的问题: 'MAMapKit/MAMapKit.h' file not found
        我修改路径也起不到作用,请问应该怎么去解决这个问题?谢谢
      • 花果山松鼠:现在出现一个问题是,<React/RCTBundleURLProvider.h>找不到路径。
        其实创建rn项目分为两种做法:1.用reactnative来创建xcode的project;(使用终端) 2.用xcode创建好project,用cocoapods安装reactnative。
        我现在用的二种方法,想模仿第一种成功的project里的代码。但是出现上面提到的错误。
        花果山松鼠:@傲娇很痛苦我现在换成了你写的podfile,但是pod的话,就报这个错: [!] No podspec found for `React` in `./react/node_modules/react-native`
        花果山松鼠:@傲娇很痛苦
        pod 'React', '~> 0.13.0-rc'
        pod 'React/Core'
        pod 'React/ART'
        pod 'React/RCTActionSheet'
        pod 'React/RCTAdSupport'
        pod 'React/RCTGeolocation'
        pod 'React/RCTImage'
        pod 'React/RCTNetwork'
        pod 'React/RCTPushNotification'
        pod 'React/RCTSettings'
        pod 'React/RCTText'
        pod 'React/RCTVibration'
        pod 'React/RCTWebSocket'
        pod 'React/RCTLinkingIOS'
        咸鱼永动机:@单车同学 你的podfile怎么写的
      • 花果山松鼠:你好,"start": "node node_modules/react-native/local-cli/cli.js start"这句话是不是有哪些需要修改的。
      • aofeilin:123456:NativeRNApp ule_zhangfanglin$ pod install
        Analyzing dependencies
        Fetching podspec for `React` from `./ReactComponent/node_modules/react-native`
        [!] Unable to satisfy the following requirements:

        - `React/Core (from `./ReactComponent/node_modules/react-native`)` required by `Podfile`

        Specs satisfying the `React/Core (from `./ReactComponent/node_modules/react-native`)` dependency were found, but they required a higher minimum deployment target.

        [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.
        柯丕安德柯丕:我的执行 pod install时也报错了
        localhost:kxdh zhuyuhui$ pod install
        Analyzing dependencies
        Fetching podspec for `React` from `./node_modules/react-native`
        [!] Unable to satisfy the following requirements:

        - `Yoga (= 0.42.0.React)` required by `React/Core (0.42.0)`

        None of your spec sources contain a spec satisfying the dependency: `Yoga (= 0.42.0.React)`.

        You have either:
        * out-of-date source repos which you can update with `pod repo update`.
        * mistyped the name or version.
        * not added the source repo that hosts the Podspec to your Podfile.

        Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default.
        咸鱼永动机:@aofeilin 这个问题我也没遇到过,尝试着模拟了一下,发现这个问题可能发生的情况是:当你的podfile中的 platform :ios, '6.0' 这个设置低于你导入第三方库的最低需求时,就会提示这个错误,你可以尝试把版本提高,再试试
        aofeilin:这是怎么原因?
      • T丶Muller:能问下你cocoapods 集成RN后, 发布release 版本。可以加载本地图片么? 我试了之后 本地图片无法加载。
        咸鱼永动机:@单车同学 报什么错呢,是说json格式上有问题吗,是不是错误定位在"start": "node node_modules/react-native/local-cli/cli.js start"这个地方?
        花果山松鼠:你好,cocoapods现在被墙了,作者步骤写的有些不清晰,我创建了package.json之后,再输入npm install不管用呢。
        咸鱼永动机:不好意思,之前文章出现了一些问题,对于图片的问题,我之前package.json中,react版本较低,文章中做了修复,可以参考一下

      本文标题:使用Cocoapods集成react native

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