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

相关文章

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

springboot2.1.3 hystrix集成及hystrix-dashboard监控详解

《springboot2.1.3hystrix集成及hystrix-dashboard监控详解》Hystrix是Netflix开源的微服务容错工具,通过线程池隔离和熔断机制防止服务崩溃,支持降级、监... 目录Hystrix是Netflix开源技术www.chinasem.cn栈中的又一员猛将Hystrix熔

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

SpringBoot集成P6Spy的实现示例

《SpringBoot集成P6Spy的实现示例》本文主要介绍了SpringBoot集成P6Spy的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录本节目标P6Spy简介抛出问题集成P6Spy1. SpringBoot三板斧之加入依赖2. 修改

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Spring Boot项目如何使用外部application.yml配置文件启动JAR包

《SpringBoot项目如何使用外部application.yml配置文件启动JAR包》文章介绍了SpringBoot项目通过指定外部application.yml配置文件启动JAR包的方法,包括... 目录Spring Boot项目中使用外部application.yml配置文件启动JAR包一、基本原理

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

SpringBoot集成Shiro+JWT(Hutool)完整代码示例

《SpringBoot集成Shiro+JWT(Hutool)完整代码示例》ApacheShiro是一个强大且易用的Java安全框架,提供了认证、授权、加密和会话管理功能,在现代应用开发中,Shiro因... 目录一、背景介绍1.1 为什么使用Shiro?1.2 为什么需要双Token?二、技术栈组成三、环境