【问题标题】:NativeScript-vue: Unable to display local HTML in WebViewNativeScript-vue:无法在 WebView 中显示本地 HTML
【发布时间】:2021-02-20 15:21:21
【问题描述】:

我正在使用 NativeScript-vue。

我想使用 WebView 在本地资源中显示 HTML 文件。

src/components/App.vue:

<template>
  <Frame>
    <Page>
      <GridLayout columns="*" rows="*">
        <WebView row="0" col="0" src="~/assets/index.html" />
      </GridLayout>
    </Page>
  </Frame>
</template>

src/main.ts:

import Vue from 'nativescript-vue';
import App from './components/App.vue';

new Vue({
  render: h => h(App)
}).$start();

src/assets/index.html:

<html>
  <body>
    <div>test</div>
  </body>
</html>

App_Resources/Android/src/main/AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="__PACKAGE__"
    android:versionCode="10000"
    android:versionName="1.0">

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name="com.tns.NativeScriptApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">

        <activity
            android:name="com.tns.NativeScriptActivity"
            android:label="@string/title_activity_kimera"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|locale|uiMode"
            android:theme="@style/LaunchScreenTheme">

            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity"/>
    </application>
</manifest>

现在,我在 Android 模拟器上运行此应用程序时收到错误“ERR_ACCESS_DENIED”。

这种情况与实际设备相同,而不是模拟器。

有什么可能的原因吗?

环境:

  • npm:
    • @nativescript/core: 7.0.3
    • nativescript-socketio:3.3.1
    • nativescript-vue: 2.8.1
  • 模拟器:API 30、Android 11.0、x86

奇怪的是,它在 Android 10.0 (API 29, x86) 模拟器上运行良好。

【问题讨论】:

  • 也许你可以查看这个答案stackoverflow.com/a/63535281/10772133
  • 感谢您的评论。可能我的 webpack 设置是正确的。 HTML 文件位于“assets”目录下,webpack 中的 CopyWebpackPlugin 设置正确。 -----> ` { from: 'assets/**', noErrorOnMissing: true, globOptions: { dot: false, ...copyIgnore } }, `

标签: android vue.js nativescript nativescript-vue


【解决方案1】:

原来在Android 11.0及以后,默认是不允许文件访问的!

Android Developers Reference: WebSettings

针对 Build.VERSION_CODES.Q 及更低版本的应用默认值为 true,而针对 Build.VERSION_CODES.R 及更高版本的应用默认值为 false。

解析代码

src/components/App.vue:

<template>
  <Frame>
    <Page>
      <GridLayout columns="*" rows="*">
        <WebView row="0" col="0" @loaded="webViewLoaded" />
      </GridLayout>
    </Page>
  </Frame>
</template>

<script lang="ts">
export default {
  methods: {
    webViewLoaded(args) {
      if (args.object.android) {
        args.object.android.getSettings().setAllowFileAccess(true); // IMPORTANT!!
        args.object.src = "~/assets/map/index.html"; // Load local HTML.
      }
    },
  },
};
</script>

【讨论】:

    猜你喜欢
    • 2019-06-14
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多