1.本文的前提条件是,电脑上已经安装了CocoaPods,React Native相关环境。

2.使用Xcode新建一个工程。EmbedRNMeituan

[图1]

3.使用CocoaPods安装React Native

在工程目录下新建Podfile文件,并配置需要使用的第三方库

pod 'React', '0.13.0-rc'
pod "React/RCTText"
pod "React/RCTActionSheet"
pod "React/RCTGeolocation"
pod "React/RCTImage"
pod "React/RCTLinkingIOS"
pod "React/RCTNetwork"
pod "React/RCTSettings"
pod "React/RCTVibration"
pod "React/RCTWebSocket"

注:如果你需要在React Native中使用<Text>,就需要添加   pod"React/RCTText”,否则不能用

 
然后安装:  pod install
 
这一步会比较慢
安装完成后,添加 Search Paths
输入$(PODS_ROOT),选择recursive

【图2】

编译一下,会报一个错,提示路径太长

Argument list too long: recursive header expansion failed at /Users/***/EmbedRNMeituan/Pods/React/node_modules/jest-cli/node_modules/istanbul/node_modules/escodegen/node_modules/estraverse.
 
这里需要修改刚才的设置,将 $(PODS_ROOT) 改成 $(PODS_ROOT)/React/React
 
再次编译,成功。

4.在工程目录下新建Components文件夹,和index.ios.js文件

【图3】

并在index.ios.js文件里粘贴一下代码:

'use strict';  

var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React; var EmbedRNMeituan = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.android.js
</Text>
<Text style={styles.instructions}>
Shake or press menu button for dev menu
</Text>
</View>
);
}
}); var styles = StyleSheet.create({
container: {
flex: ,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: ,
textAlign: 'center',
margin: ,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: ,
},
}); AppRegistry.registerComponent('EmbedRNMeituan', () => EmbedRNMeituan);

以上,React Native部分已经弄完。下面开始原生部分。

5.新建显示React Native的UIView。
 
用来加载显示React Native的容器是 RCTRootView,它是继承自UIView。
 
在ViewController.m中

#import "RCTRootView.h"  

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. [self initRNView];
} -(void)initRNView {
NSURL *jsCodeLocation;
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"EmbedRNMeituan"
initialProperties:nil
launchOptions:nil];
//注意,这里是 @"EmbedRNMeituan"
rootView.frame = CGRectMake(, , , );
[self.view addSubview:rootView];
}
6.运行
 此时如果运行的话,会出现下面的情况

【图4】

提示找不到服务器,以及数据传输的安全问题。

6.1允许http请求
打开info.plist文件,添加

【图5】

App Transport Security Settings    -》 Dictionary
          Allow Arbitrary Loads          -》 YES
6.2启动服务器
工程目录下,终端输入:

(cd Pods/React; npm run start)

6.3编写脚本文件run.sh

vi run.sh

输入

#! /bin/bash
(cd Pods/React; npm run start)

然后给run.sh添加可执行权限:chmod +x run.sh

之后开启服务器时,只需要在终端输入命令: ./run.sh
 
链接:

iOS原生项目中集成React Native的更多相关文章

  1. react native 之 在现有的iOS工程中集成react native

    在现有的iOS工程中集成react native, 或者说将react native引入到iOS 项目,是RN和iOS混合开发的必经之路 参考官网教程:https://reactnative.cn/d ...

  2. iOS原生项目集成React Native模块

    今天周末,弄弄Native和React Native之间的交互.首先,先在iOS原生项目中集成React Native模块: 注意事项: 1.因为react native的版本问题,部分细节可能有所不 ...

  3. 现有iOS项目集成React Native过程记录

    在<Mac系统下React Native环境搭建>配置了RN的开发环境,然后,本文记录在现有iOS项目集成React Native的过程,官方推荐使用Cocoapods,项目一开始也是使用 ...

  4. Android Studio 项目中集成百度地图SDK报Native method not found: com.baidu.platform.comjni.map.commonmemcache.JNICommonMemCache.Create:()I错误

    Android Studio 项目中集成百度地图SDK报以下错误: java.lang.UnsatisfiedLinkError: Native method not found: com.baidu ...

  5. 现有项目中集成Flutter

    本文列举了项目开发使用Flutter会遇到的问题,以及如何使用Flutter module在现有项目中集成Flutter,并对其原理进行了分析. 最近在做的一个商业项目,完全的使用Flutter编写的 ...

  6. unity导出工程导入到iOS原生工程中详细步骤

    一直想抽空整理一下unity原生工程导入iOS原生工程中的详细步骤.做iOS+vuforia+unity开发这么长时间了.从最初的小小白到现在的小白.中间趟过了好多的坑.也有一些的小小收货.做一个喜欢 ...

  7. 在ASP.NET MVC项目中使用React

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:最近在开发钉钉的微应用,考虑到性能和UI库的支持,遂采用了React来开发前端. 目前 ...

  8. SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL

    1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...

  9. Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

随机推荐

  1. 【读书笔记】iOS网络-底层网络

    在iOS上,有一个库叫做Core Foundation networking或CFNetwork,它是对原始Socket的轻量级封装,不过它很快对于大多数常见场景来说变得非常笨重了.最后,添加了另一层 ...

  2. ios xcode Code signing failed 解决方案

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545; min ...

  3. 单例模式-用GCD实现

    用GCD实现单例模式的步骤: 步骤1. 创建头文件 XZSingleton.h,里面代码如下: // .h文件 #define XZSingletonH(name) + (instancetype)s ...

  4. asp.net mvc SelectList 的selected 失效及解决方案

    ViewData 名 不能和 绑定的 DropdownListFor的字段名 重复  

  5. 入CTF坑必不可少的地方-保持更新

    0x00 前言 没有交易,没有买卖,没有排名,纯属分享:p 0x01 CTF介绍 CTF领域指南CTF介绍大全CTF赛事预告 0x02 CTF练习 BIN:reversingpwnableexploi ...

  6. stringstream操纵string小总结

    1 split字符串 之前在用C#写代码的时候,用过split函数,可以把一个字符串根据某个分隔符分成若干个字符串数组.在用C++操纵字符串的时候,我一直使用很笨的遍历的方法.为此,我问候过很多次C+ ...

  7. 【转】探索C#之布隆过滤器(Bloom filter)

    原文:蘑菇先生,http://www.cnblogs.com/mushroom/p/4556801.html 背景介绍 Bloom filter(后面简称BF)是Bloom在1970年提出的二进制向量 ...

  8. ORACLE关于索引是否需要定期重建争论的整理

    ORACLE数据库中的索引到底要不要定期重建呢? 如果不需要定期重建,那么理由是什么? 如果需要定期重建,那么理由又是什么?另外,如果需要定期重建,那么满足那些条件的索引才需要重建呢?关于这个问题,网 ...

  9. Windows Server 2012 Recycle Bin corrupted

    在Windows Server 2012 上遇到了“The Recycle Bin On E:\ is corrupted. Do you want to empty the Recycle Bin ...

  10. Servlet/JSP-03 HttpServlet

    一. GenericServlet GenericServlet本身是一个抽象类,并且实现了Servlet和ServletConfig接口 其在内部定义了一个私有的ServletConfig类型的变量 ...