我们可以在android WebView中保存登录数据吗?

时间:2018-01-10 20:18:54

标签: android login webview

我的网站需要登录才能使用它,所以有没有办法为用户保存登录数据,这样他们就不会在每次进入WebView时登录,如firebase或cookies等? 如果您需要分享我的代码,请告诉我。

谢谢。

修改1:

这是MainActivity.java文件代码:

package com.company.app;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

WebView webView;
SwipeRefreshLayout swipe;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.webView).setVisibility(View.GONE);

    swipe = (SwipeRefreshLayout)findViewById(R.id.swipe);
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            WebAction();
        }
    });

    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);

    WebView webview = new WebView(this);
    WebSettings ws = webview.getSettings();
    ws.setSaveFormData(true);
    ws.setSavePassword(true); // Not needed for API level 18 or greater (deprecated)


    WebAction();



}

public void WebAction(){

    webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);

    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // chromium, enable hardware acceleration
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else {
        // older android version, disable hardware acceleration
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }


    String myURL = "https://www.myURL.com/";

    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);

    WebView webview = new WebView(this);
    WebSettings ws = webview.getSettings();
    ws.setSaveFormData(true);
    ws.setSavePassword(true); // Not needed for API level 18 or greater (deprecated)


    webView.loadUrl(myURL);
    swipe.setRefreshing(true);
    webView.setWebViewClient(new WebViewClient(){

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            webView.loadUrl("file:///android_asset/error.html");

        }

        public void onPageFinished(WebView view, String url) {
            // do your stuff here
            swipe.setRefreshing(false);

            findViewById(R.id.webView).setVisibility(View.VISIBLE);


        }

    });

}


@Override
public void onBackPressed(){

    if (webView.canGoBack()){
        webView.goBack();
    }else {
        finish();
    }
}

}

2 个答案:

答案 0 :(得分:4)

我找到了答案。

我们只需要添加this.webView.getSettings().setDomStorageEnabled(true);this.webView.getSettings().setJavaScriptEnabled(true);

现在我们可以删除cookie代码,并保存数据和登录。

修改 如果它不起作用,请尝试不删除cookie代码。

答案 1 :(得分:0)

尝试通过javascript注入获取值

webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            webView.loadUrl(
                "javascript:(function() { " +
                    "var element = document.getElementById('hplogo');"
                    + "element.parentNode.removeChild(element);" +
                "})()");
        }
    });