【问题标题】:Is there a way to disable all caching in WebView (both iOS and Andriod), in a NativeScript Angular app?有没有办法在 NativeScript Angular 应用程序中禁用 WebView(iOS 和 Andriod)中的所有缓存?
【发布时间】:2019-08-07 08:02:10
【问题描述】:

我正在制作一个 NativeScript Angular 应用程序,它使用 WebView 来显示远程网页,但是当我在网站上的这个页面上进行更改时,它不会在 Andriod 和我用于应用程序开发的 iOS 设备上得到更新.页面在这些设备上的常规浏览器上正常更新。

我已经阅读了 NativeScript Angular 文档中关于 WebView 控件的说明,但不是很详细。

我希望能够做类似的事情:

    mWebView.getSettings().setAppCacheEnabled(false);
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

...如here 所述。

【问题讨论】:

    标签: caching webview nativescript


    【解决方案1】:

    您可以通过以下方式获取对 NativeScript WebView 的引用并从那里访问原生 Android 控件 (android.webkit.WebView)

    import { Component } from "@angular/core";
    import { EventData } from "tns-core-modules/data/observable";
    import { WebView } from "tns-core-modules/ui/web-view";
    import { isAndroid } from "tns-core-modules/platform";
    
    declare let android: any; // or even better - use tns-platform-declarations for intelliSense for the native APis
    
    @Component({
        selector: "Home",
        moduleId: module.id,
        templateUrl: "./home.component.html",
        styleUrls: ["./home.component.css"]
    })
    export class HomeComponent {
    
        onWebViewLoaded(args: EventData) {
            const webView = args.object as WebView;
    
            const nativeWebView = webView.nativeView; // equal to webView.android or webView.ios (depending on the platform)
    
            if (isAndroid) {
                nativeWebView.getSettings().setAppCacheEnabled(false);
                nativeWebView.getSettings().setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);
            }
        }
    }
    

    其中 onWebViewLoaded 与 HTML 中的加载事件一起使用

    <WebView height="1200" src="https://www.nativescript.org" (loaded)="onWebViewLoaded($event)"></WebView>
    

    一个展示上述here的Playground应用程序

    【讨论】:

    • 请参考this thread 获取 iOS 中的原生 API 以清除缓存
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2022-01-04
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多