1

My website needs to be logged in to use it, so is there a way to save login data for the users so they will not log in every time they enter the WebView, like firebase, or cookies, etc? If you need to share my code, please let me know.

Thank you.

Edit 1:

This is MainActivity.java file code:

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();
    }
}

}

مشترياتي
  • 69
  • 1
  • 1
  • 7
  • To be clear, you have a `WebView` in your app that the user uses to access your website right? – Brian Jan 10 '18 at 21:12
  • Yes, I use webView in my app. – مشترياتي Jan 11 '18 at 05:21
  • From my understanding, `WebViews` now automatically persist cookies so could you just implement using cookies like what most websites would do for storing login sessions? – Brian Jan 11 '18 at 07:10
  • I add a code to the app to accept cookies but it does not work. – مشترياتي Jan 11 '18 at 10:19
  • I will add the code to the question so you can take a look if something wrong. – مشترياتي Jan 11 '18 at 17:00
  • I was looking at the docs (https://stackoverflow.com/a/34892588/394933) for `CookieSyncManager` and it looks like you might need to call some `startSync()` method to make things work? Alternatively, if you're using API 21+ after they deprecated `CookieSyncManager`, it looks like you can use the `CookieManager` singleton to also force syncing, check out: https://stackoverflow.com/a/34892588/394933. Does that help? – Brian Jan 11 '18 at 23:36
  • It doesn't work. – مشترياتي Jan 12 '18 at 04:59
  • Thanks, Your settings works for me by the way :) – Sandeep Mar 28 '20 at 07:47

2 Answers2

5

I found the answer.

We just need to add this.webView.getSettings().setDomStorageEnabled(true); along with this.webView.getSettings().setJavaScriptEnabled(true);.

Now we can delete the cookies codes, and save data and logins.

EDIT: If it doesn't work, try not to delete the cookies code.

مشترياتي
  • 69
  • 1
  • 1
  • 7
0

try to get values by javascript injection

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);" +
                "})()");
        }
    });
AhmetAcikalin
  • 328
  • 2
  • 7