1

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

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

4

2 回答 2

4

在您启用它们之前,Android localStorageAPI 将无法使用。您可以通过在加载事件时访问本机 WebView 的设置来启用它。

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

游乐场样本

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

于 2019-02-07T16:38:32.970 回答
2

很好,谢谢!!该示例运行良好。

.html

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

-组件(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();
    }
}
于 2019-02-08T08:29:43.490 回答