react native webview加载本地HTML,解决iOS无法加载成功问题

在react native中使用 "react-native-webview": "^13.13.5",加载HTML文件

Android:

将HTML文件放置到android/src/main/assets目录,访问

bash 复制代码
{uri: 'file:///android_asset/markmap/index.html'}

ios:

在IOS中可以直接可以直接放在react native项目下,访问方式如下

bash 复制代码
require('../../assets/markmap.html')

这里遇到一个问题是编译出来的HTML文件中带有单独的js和CSS的时候在iOS中无法加载成功,解决方法是用vite-plugin-singlefile将前端项目导出为单独文件,我的vite.config.js配置如下:

bash 复制代码
import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react'
import {viteSingleFile} from "vite-plugin-singlefile";


// https://vitejs.dev/config/
export default defineConfig({
    plugins: [react(),viteSingleFile()],
    base: './', // 设置为相对路径

})

如果是其他的打包方式也实现同样的功能就行。

完整的代码:

bash 复制代码
           <WebView
                ref={webViewRef}
                source={Platform.OS==='android'?{uri: 'file:///android_asset/markmap/index.html'}:require('../../assets/markmap.html')}
                style={styles.webView}
                originWhitelist={['*']}
                javaScriptEnabled={true}
                domStorageEnabled={true}
                allowFileAccess={true}
                allowFileAccessFromFileURLs={true}
                allowUniversalAccessFromFileURLs={true}

                onMessage={handleMessage}/>
相关推荐
二流小码农31 分钟前
鸿蒙开发:个人开发者如何使用华为账号登录
android·ios·harmonyos
Access开发易登软件1 小时前
Access开发实战:绘制漏斗图实现业务转化分析
数据库·信息可视化·html·vba·图表·access
光影少年2 小时前
RN vs Flutter vs Expo 选型
前端·flutter·react native
程序员刘禹锡4 小时前
Html中常用的块标签!!!12.16日
前端·html
wvy4 小时前
Xcode 26还没有适配SceneDelegate的app建议尽早适配
ios
狮智先生4 小时前
【编程实践】PySide6 + Plotly + Pandas 开发HTML数据分析报告
程序人生·plotly·html·pandas
蜗牛攻城狮4 小时前
Vite 项目中 `node_modules/.vite/deps` 文件夹详解
前端·vite·构建工具
游戏开发爱好者85 小时前
苹果 App 上架流程,结合 Xcode、CI 等常见工具
macos·ios·ci/cd·小程序·uni-app·iphone·xcode
GDAL5 小时前
HTML 实现登录状态记录 深入全面讲解教程
前端·html·登录验证
前端老白5 小时前
webview在微信小程序中,安卓加载失败,IOS正常加载
android·ios·微信小程序·webview