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}/>
相关推荐
codingWhat3 小时前
小程序里「嵌」H5:一套完整可落地的 WebView 集成方案
前端·uni-app·webview
ssshooter4 小时前
Tauri 踩坑 appLink 修改后闪退
前端·ios·rust
天蓝色的鱼鱼7 小时前
都2026年了还不会Vite插件开发?手写一个版本管理插件,5分钟包会!
前端·vite
二流小码农8 小时前
鸿蒙开发:上传一张参考图片便可实现页面功能
android·ios·harmonyos
开心就好20251 天前
UniApp开发应用多平台上架全流程:H5小程序iOS和Android
后端·ios
开心就好20251 天前
免 Xcode 的 iOS 开发新选择?聊聊一款更轻量的 iOS 开发 IDE kxapp 快蝎
后端·ios
恋猫de小郭1 天前
Apple 的 ANE 被挖掘,AI 硬件公开,宣传的 38 TOPS 居然是"数字游戏"?
前端·人工智能·ios
willow1 天前
html5基础整理
html
Live000002 天前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
忆江南2 天前
iOS 深度解析
flutter·ios