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}/>
相关推荐
Zender Han19 分钟前
Flutter 视频播放器——flick_video_player 介绍与使用
android·flutter·ios·音视频
Yvonne爱编码38 分钟前
AJAX入门-AJAX 概念和 axios 使用
前端·javascript·ajax·html·js
果壳~1 小时前
【Python】爬虫html提取内容基础,bs4
爬虫·python·html
咕噜签名分发冰淇淋3 小时前
苹果ios的系统app应用WebClip免签应用开源及方式原理
ios·开源·cocoa
2501_915106326 小时前
App Store 软件上架全流程详解,iOS 应用发布步骤、uni-app 打包上传与审核要点完整指南
android·ios·小程序·https·uni-app·iphone·webview
开开心心loky6 小时前
[iOS] ViewController 的生命周期
macos·ui·ios·objective-c·cocoa
机构师9 小时前
<uniapp><指针组件>基于uniapp,编写一个自定义箭头指针组件
javascript·uni-app·vue·html
2501_9160137411 小时前
App 上架全流程指南,iOS App 上架步骤、App Store 应用发布流程、uni-app 打包上传与审核要点详解
android·ios·小程序·https·uni-app·iphone·webview
牛蛙点点申请出战11 小时前
仿微信语音 WaveView 实现
android·前端·ios
诗书画唱12 小时前
解决HTML/JS开发中的常见问题与实用资源
前端·javascript·html