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}/>
相关推荐
lili-felicity2 小时前
React Native 鸿蒙跨平台开发:LayoutAnimation 实现鸿蒙端按钮点击的缩放反馈动画
react native·react.js·harmonyos
3824278272 小时前
表单提交验证:onsubmit与return详解
前端·javascript·html
哈__4 小时前
React Native 鸿蒙跨平台开发:Dimensions 屏幕尺寸获取
react native·华为·harmonyos
哈__5 小时前
React Native 鸿蒙跨平台开发:下拉刷新功能
javascript·react native·react.js
川石课堂软件测试5 小时前
Android和iOS APP平台测试的区别
android·数据库·ios·oracle·单元测试·测试用例·cocoa
liusheng5 小时前
Capacitor + React 的 iOS 侧滑返回手势
前端·ios
2501_915918416 小时前
除了 Perfdog,如何在 Windows 环境中完成 iOS App 的性能测试工作
android·ios·小程序·https·uni-app·iphone·webview
哈__6 小时前
基础入门 React Native 鸿蒙跨平台开发:TabBar 底部导航栏
javascript·react native·react.js
lili-felicity6 小时前
React Native 鸿蒙跨平台开发:Animated 实现鸿蒙端组件的左右滑动动画
javascript·react native·react.js