ReactNative集成到已有iOS项目

2024-05-31 14:28

本文主要是介绍ReactNative集成到已有iOS项目,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在维护一个项目,需要引入ReactNative开发新UI。记录一下过程,表示我搞过。V

需要安装node环境

brew install node
npm install -g yarn

接着创建一个目录用来保存工程文件,并在目录项目下面创建一个package.json文件

mkdir demo
cd demo
npm init

加下来安装react native必须的库, 0.71.7版本是支持xcode14.2的,大于这个版本就需要xcode14.3了,我的电脑是MBP 2015 Mid,只能升级到xcode14.2,所以也只能用这个react-native版本。

yarn add react-native@0.71.7
yarn add react@18.2.0

react native部分就准备好了,接下来我们配置iOS部分,首先在demo目录下创建一个ios目录,把iOS源码copy到这个ios目录里面。接下来需要cocoapods的配置,编辑Podfile,在里面添加
require_relative ‘…/node_modules/react-native/scripts/react_native_pods’
require_relative '…/node_modules/@react-native-community/cli-platform-ios/native_modules’与use_react_native!。下面是一个Sample

# Uncomment the next line to define a global platform for your project
# platform :ios, '13.0'require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'target 'App' do# Comment the next line if you don't want to use dynamic frameworksuse_frameworks!use_react_native!# Pods for Append

接下来

pod install

这下工程准备好了。

native加载react native页面

接下来我们用react native来写一个页面。再demo目录创建一个index.js文件,内容如下

import React from 'react';
import {AppRegistry, StyleSheet, Text, View} from 'react-native';const RNHighScores = ({scores}) => {const contents = scores.map(score => (<Text key={score.name}>{score.name}:{score.value}{'\n'}</Text>));return (<View style={styles.container}><Text style={styles.highScoresTitle}>2048 High Scores!</Text><Text style={styles.scores}>{contents}</Text></View>);
};const styles = StyleSheet.create({container: {flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#FFFFFF',},highScoresTitle: {fontSize: 20,textAlign: 'center',margin: 10,},scores: {textAlign: 'center',color: '#333333',marginBottom: 5,},
});// Module name
AppRegistry.registerComponent('RNHighScores', () => RNHighScores);

这是react native开发的页面, 注册的组件名是RNHighScores。接下来我们再iOS native端来显示这个页面。
创建一个UIViewController.

//
//  MyRNViewController.swift
//  App
//
//  Created by Haven Tang on 2024/5/27.
//  https://reactnative.dev/docs/0.73/integration-with-existing-apps
//  https://www.youtube.com/watch?v=3wftC30CN2I
//  https://nishabe.medium.com/how-to-integrate-react-native-code-with-an-existing-ios-app-655c61a65b8c
// https://fbflipper.com/docs/getting-started/ios-native/
// https://fbflipper.com/docs/getting-started/react-native/import UIKit
import Reactclass MyRNViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.}override func loadView() {loadReactView()}func loadReactView() {let bundleUrl = URL(string: "http://localhost:8081/index.bundle?platform=ios")let mockData: NSDictionary = ["scores":[["name":"Alex", "value":"42"],["name":"Joel", "value":"10"]]]let rootView = RCTRootView(bundleUrl: bundleUrl, moduleName: "RNHighScores", initialProperties: mockData as [NSObject : AnyObject], launchOptions: nil)self.view = rootView}
}

显示这个view

@IBAction func showRNScoresView(_ sender: Any) {let vc = MyRNViewController()self.present(vc, animated: true)}

接下来运行App验证一下, 这儿我们使用的Metro bundler是本地http://localhost的一个服务,需要网络访问,所以记得再plist中开启运行http访问网络。

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

开启react native的Metro bundler server

npm start

运行App,点击xcode运行,或者命令行运行App

# From the root of your project
$ npx react-native run-ios

如果发布App的时候,我们需要将Metro bundler打包到App bundle里面。再package.json的scripts里面加上

"bundle-ios": "react-native bundle --entry-file index.js --bundle-output ./ios/bundle/main.jsbundle --platform ios --assets-dest ./ios/bundle --dev false",
"bundle-android": "react-native bundle --entry-file index.js --bundle-output ./android/app/src/main/assets/main.bundle --platform android --assets-dest ./android/app/src/main/res --dev false",

运行下面命令打包ios bundle

yarn bundle-ios

查看ios/bundle目录下文件,将其拖入xcode工程,这样就可以直接加载bundle中的资源了。
对应的bundleUrl需要调整一下:

let bundleUrl = NSBundle(URLForResource: "main" withExtension:"jsbundle")

react native加载native页面

要实现React native直接使用Native中的页面,可以使用React Native 的Native Component技术。后续会被Fabric 原生组件 替代。

源码下载

这篇关于ReactNative集成到已有iOS项目的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1018177

相关文章

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的

springboot集成Deepseek4j的项目实践

《springboot集成Deepseek4j的项目实践》本文主要介绍了springboot集成Deepseek4j的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录Deepseek4j快速开始Maven 依js赖基础配置基础使用示例1. 流式返回示例2. 进阶

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于

SpringBoot项目使用MDC给日志增加唯一标识的实现步骤

《SpringBoot项目使用MDC给日志增加唯一标识的实现步骤》本文介绍了如何在SpringBoot项目中使用MDC(MappedDiagnosticContext)为日志增加唯一标识,以便于日... 目录【Java】SpringBoot项目使用MDC给日志增加唯一标识,方便日志追踪1.日志效果2.实现步

Spring Boot 集成 Quartz 使用Cron 表达式实现定时任务

《SpringBoot集成Quartz使用Cron表达式实现定时任务》本文介绍了如何在SpringBoot项目中集成Quartz并使用Cron表达式进行任务调度,通过添加Quartz依赖、创... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启

Ubuntu中Nginx虚拟主机设置的项目实践

《Ubuntu中Nginx虚拟主机设置的项目实践》通过配置虚拟主机,可以在同一台服务器上运行多个独立的网站,本文主要介绍了Ubuntu中Nginx虚拟主机设置的项目实践,具有一定的参考价值,感兴趣的可... 目录简介安装 Nginx创建虚拟主机1. 创建网站目录2. 创建默认索引文件3. 配置 Nginx4