react-native-webview ios加載本地js、css靜態資源

最近用react-native做一個學生學習的app,項目當中需要展示pdf,我是用webview加載一個本地html,然後用pdf.js來渲染pdf的。至於,爲什麼不用react-native-pdf ,這裏不討論。
用到的靜態資源有 jquery.js、pdf.js、pdf.worker.js,都需要在html中用<script src=’…’></scirpt> 標籤引用。

以前項目中的做法是
android把資源放在 /android/app/src/main/assets 目錄下,ios把資源放到組件的同級目錄下即可,然後在項目中引用

const source = Platform.OS === 'ios' ? require('./index.html) : {'uri':'file:///android_asset/pdfView/index.html'}
// pdfView是我存放靜態資源的文件夾
// 該文件夾放在/android/app/src/main/assets 目錄下
<WebView source={source}/>

這樣寫沒有任何問題,ios和android都能夠加載資源,但這次使用的是[email protected][email protected],在android下沒有任何問題,但在ios下問題就來了。ios下,三個js靜態資源無法加載html裏面,導致無法執行PDFJS方法,pdf也就不能展示出來。

這種情況我門可以像android一樣把靜態資源放在ios目錄下,然後用baseUrl設置靜態資源訪問路徑就能訪問到了,具體做法如下

const html = `<html>
	<head>
		<meta charset="UTF-8">
		<script src="jq.js" type="text/javascript"></script>
		<script src="pdf.js" type="text/javascript"></script>
	</head>
	<body>
		<div>test</div>
	</body>
</html>`
const source = Platform.OS === 'ios' ? {html, baseUrl: 'pdfweb/'} : {'uri':'file:///android_asset/pdfView/index.html'}
<WebView source={source}/>

將資源文件放到pdfView目錄下,用xcode將目錄導入到項目中,如下
在這裏插入圖片描述
然後重啓項目就ok啦


歡迎各位看官留言討論,我看到會及時回覆!

各位看官,如您覺得這篇博文對您有點用處,打賞幾個銅板吧,俺會再接再厲繼續努力,若您覺得這篇文章寫的太菜,請輕噴,程序猿混口飯喫不容易!!!

支付寶 微信
在這裏插入圖片描述 在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章