在iOS原生功能中加入RN,请看之前 写的 RN与iOS交互系列文章.本篇只讲下视图混编.

关键点只有二:

1.通过 RCTRootView 加载RN视图.

2.RN中,只需要AppRegistry.registerComponent()导出即可.

关键代码:

OC里面:

//
// RootViewController.m
// iOSRN
//
// Created by Shaoting Zhou on 2017/12/8.
// Copyright © 2017年 Shaoting Zhou. All rights reserved.
// #import "RootViewController.h"
#import <React/RCTRootView.h>
#define Main_Screen_Height [[UIScreen mainScreen] bounds].size.height
#define Main_Screen_Width [[UIScreen mainScreen] bounds].size.width
@interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {
[self loadIOSView];
[self loadRNView]; }
#pragma mark - 添加IOS视图
-(void)loadIOSView{
self.view.backgroundColor = [UIColor redColor]; NSString * strUrl = @"http://localhost:8081/App.bundle?platform=ios&dev=true";
NSURL * jsCodeLocation = [NSURL URLWithString:strUrl];
RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"iosrn"
initialProperties:nil
launchOptions:nil];
rootView.frame = CGRectMake(, , , ); [self.view addSubview:rootView]; } #pragma mark - 添加rn视图
-(void)loadRNView{
[super viewDidLoad];
NSString * strUrl = @"http://localhost:8081/index.bundle?platform=ios&dev=true";
NSURL * jsCodeLocation = [NSURL URLWithString:strUrl]; RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"iosrn"
initialProperties:nil
launchOptions:nil];
rootView.frame = CGRectMake(, Main_Screen_Height/, Main_Screen_Width, Main_Screen_Height/); UILabel * la = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
la.backgroundColor = [UIColor blueColor];
la.text = @"我是原生";
[rootView addSubview:la]; [self.view addSubview:rootView]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

RN里面:

import React, { Component } from 'react';
import {
AppRegistry,
Platform,
StyleSheet,
Text,
View
} from 'react-native'; const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
}); export default class App extends Component<{}> {
render() {
return (
<View style={styles.container}>
<Text>
我是RN
</Text>
</View>
);
}
} const styles = StyleSheet.create({
container: {
flex: ,
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: ,
textAlign: 'center',
margin: ,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: ,
},
}); AppRegistry.registerComponent('iosrn', () => App);

效果如图:

github:  https://github.com/pheromone/IOS-native-and-React-native-interaction  中的 iOSRN

iOS原生 和 react native视图混编的更多相关文章

  1. 一个资深iOS开发者对于React Native的看法

    一个资深iOS开发者对于React Native的看法 当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道.   我认为一个js开发者可以使用javasc ...

  2. iOS 写给iOS开发者的React Native学习路线(转)

    我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...

  3. 写给iOS开发者的React Native学习路线(转)

    我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...

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

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

  5. 原生工程接入Flutter实现混编

    前言 上半年我定的OKR目标是帮助团队将App切入Flutter,实现统一技术栈,变革成多端融合开发模式.Flutter目前是跨平台方案中最有潜力实现我们这个目标的,不管是Hybird还是React ...

  6. [转] 一个资深iOS开发者对于React Native的看法

    当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道. 我认为一个js开发者可以使用javascript来构建iPhone应用确实是一件很酷的事情,但是我很 ...

  7. Android原生嵌入React Native

    1.首先集成的项目目录 我使用的是直接按照react-native init Project 的格式来导入的,也就是说,我的Android项目目录是跟node_modules是在一个目录下的. 我们i ...

  8. 《React Native 精解与实战》书籍连载「iOS 平台与 React Native 混合开发」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  9. 【React Native】在原生和React Native间通信(RN调用原生)

    一.从React Native中调用原生方法(原生模块) 原生模块是JS中也可以使用的Objective-C类.一般来说这样的每一个模块的实例都是在每一次通过JS bridge通信时创建的.他们可以导 ...

随机推荐

  1. ch.ethz.ssh2.Session和com.jcraft.jsch.Session

    通过Jsch连接step 1引入jar包<!-- jcraft包 -->        <dependency>            <groupId>com.j ...

  2. 常用算法和Demo(Java实现)(持续更新)

    源码地址:https://github.com/zwxbest/Demo 1.顺序存储和单向链表,双向链表,循环链表的增删查改 https://github.com/zwxbest/Demo/tree ...

  3. 2017年11月23日**科技面试java工程师面试总结

    从整个面试看了,表现并不理想,有很多不足. 1.关于手机页面闪现问题的处理.从原理上观察,页面闪现是因为异步请求导致的.在进入页面中首先会加载默认的东西,此时也会访问数据库端,会用ajax判断是否满足 ...

  4. UIPath Level 2&3

    Level 3 走了很多弯路,但是学到了很多东西,贴一个Level3的吧,其他的省略了 认认真真独立做完Level3的两个POC,相信你对UIPath的理解会更深入一步 晚安,祝各位中秋节快乐!

  5. 基于VC++的网络扫描器设计与实现

    本文正文其实是自己的毕业论文,现在搬上来有两个原因. 一是之前大学的文档都放在百度网盘上,大概去年的时候百度网盘改版搞得不太稳定,文件夹移动次数一多里边就会有一些文件丢失了,也没有很在意.但前几天看申 ...

  6. 【Monkey】Monkey获取包名的方式

    获取包名的方式: 1.有源码:查看AndroidManifest.xml 2.只有apk: 2.1 aapt dump xmltree ColaBox.apk AndroidManifest.xml ...

  7. Yarn架构详解

    Yarn架构介绍Yarn/MRv2最基本的想法是将原JobTracker主要的资源管理和job调度/监视功能分开作为两个单独的守护进程.有一个全局的ResourceManager(RM)和每个Appl ...

  8. Rhino学习教程——1.4

    状态栏 状态栏位于整个工作界面的下方,主要显示了一些系统操作时的信息. 根据不同的功能,可以将状态栏分成4个部分. 一.坐标系统 在状态栏左侧显示了当前所使用的坐标系统(“世界”或“工作平面”,可以通 ...

  9. "No entity found for query"(转)

    很久以前一直用EntityManager下getResultList()查询数据,即便一个数据亦是如此,所以要频繁的List.get(0).新接口getSingleResult()出现了,然后就开始频 ...

  10. Vue的自定义组件之间的数据传递

    一,父级传向子级 1,在子级的属性中添加props:['myname',......],参数可以传多个,看具体而定: 2,在父级data中定义好需要传递的变量数据,例如name:"rose& ...