本地存储API在NativeScript WebView中不起作用

时间:2019-02-07 15:33:43

标签: android local-storage nativescript

我的应用程序是一个托管在{N} WebView中的Web应用程序。用户名和密码存储在localStore中。当我离开应用程序并重新输入时,我没有数据,我必须返回登录。

有人可以帮我吗?或说明如何在应用程序中调试Web视图。 该网页是使用Angular 4开发的,而应用是使用NativeScript开发的。

2 个答案:

答案 0 :(得分:1)

使用Android localStorage API之前,必须先启用它们。您可以通过在加载事件时访问本地WebView的设置来启用它。

export function onWebViewLoaded(args) {
    const webView = args.object;
    if (webView.android) {
        webView.android.getSettings().setDomStorageEnabled(true);
        webView.android.getSettings().setDatabaseEnabled(true);
    }
}

Playground Sample

在这之间,是否可以localStorage保存机密信息(例如用户名/密码)是另一个问题。我的回答是“否”,因为它很容易/易于使用,所以您不能使用它。通常,您应该使用用户提供的这些凭据来访问登录api,您可能应该获得access token,随后将用于与服务器的任何通信。您可以将此令牌存储在localStoage中,注销后将其清除。

答案 1 :(得分:0)

完美,谢谢!! 这个例子很好用。

.html

<Page (loaded)="pageLoaded($event)">
        <GridLayout rows="*" columns="*">
           <WebView  #myWebView [src]="webViewSrc" (loadStarted)="onWebViewLoadStarted($event)"></WebView>
        </GridLayout>
</Page>

-component(ts文件)

import { Component, OnInit, AfterViewInit, ViewChild, ElementRef } from "@angular/core";

@Component({
    selector: "Home",
    moduleId: module.id,
    templateUrl: "./home.component.html",
    styleUrls:['./home.component.css']
})
export class HomeComponent  {
   public webViewSrc: string = URL;
    constructor(private routerExtensions: RouterExtensions) {
        // Use the component constructor to inject providers.
    }

    onWebViewLoaded(args){
        const webView = args.object;
        if (webView.android) {
            webView.android.getSettings().setDatabaseEnabled(true);
            webView.android.getSettings().setLoadWithOverviewMode(true);
        }
    }
    pageLoaded(args: EventData) {
        let page = <Page>args.object;
        page.bindingContext = new HomeViewModel();
    }
}
相关问题