在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(100, 100, 50, 50); [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(0, Main_Screen_Height/2, Main_Screen_Width, Main_Screen_Height/2); UILabel * la = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 100)]; 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: 1, backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, }); AppRegistry.registerComponent('iosrn', () => App);
效果如图:
github: https://github.com/pheromone/IOS-native-and-React-native-interaction 中的 iOSRN
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2015-12-08 iOS一些系统事件的生命周期