Save user and password, cookie in a webview

Asked

Viewed 267 times

-1

How can I save a user and password in the android webview? On the page (web), it is requested Cpf and password, for login. After this entry, the cookie is saved vis_cod, which is a return parameter created (on the web) from the password cpfe that were entered. How to save this parameter, in webview and access the app without the need to enter Cpf and password, always?

public class fanpageActivity extends AppCompatActivity {
    private WebView mywebView;

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

        mywebView = (WebView) findViewById(R.id.webview2);

        mywebView.getSettings().setJavaScriptEnabled(true);
        mywebView.getSettings().setDomStorageEnabled(true);

        mywebView.loadUrl("http://mydomain.com/domain");
        mywebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                ProgressBar pb = (ProgressBar) findViewById(R.id.progress);
                pb.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                ProgressBar pb = (ProgressBar) findViewById(R.id.progress);
                pb.setVisibility(View.INVISIBLE);
                mywebView.setVisibility(View.VISIBLE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(final WebView myWebview, String url) {

                //Abrir telefone
                if (url.contains("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                    startActivity(intent);
                    myWebview.reload();
                    return false;
                }
                //Abrir Whatsapp
                else if (url.contains("api.whatsapp")) {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                    myWebview.reload();
                    return false;
                } else {
                    myWebview.loadUrl(url);
                    return true;
                }

/*                myWebview.loadUrl(url);
                return false;*/
            }

        });

    }

1 answer

-1

I don’t remember if the webview allows the storage of cookies. I quickly looked at the documentation and found nothing like it, but there may be. But, a palliative solution can be you connect Javascript from your page to Java, and there you save the data in the preferences. Then at the beginning of your HTML page you make Javascript take the data, see if they are null and set in the fields and click the button. Follow a possible example!


//MainActivity.java

import android.app.*;
import android.os.*;
import android.webkit.*;
import android.widget.*;
import android.preference.*;
import android.content.*;

public class MainActivity extends Activity
{
    private WebView webview;

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

        webview = findViewById(R.id.webview);
        WebSettings ws = webview.getSettings();
        ws.setJavaScriptEnabled(true);
        JI ji = new JI(); //Declaração da classe feita abaixo
        webview.addJavascriptInterface(ji, "JI");
        //Neste ponto você pode substituir pelo seu site eu fiz assim para facilitar o exemplo. O código do site está sendo retornado pelo método.
        webview.loadData(getHtml(), "text/html", "UTF-8");
    }

    private String getHtml(){
        String html = new String();
        html += "<!DOCTYPE html>";
        html += "<html>";
            html += "<head>";
                html += "<meta charset=\"UTF-8\"/>";
                html += "<title>Página Teste</title>";
            html += "</head>";
            html += "<body>";
                html += "<form>";
                    html += "Login: <input type=\"text\" id=\"login\"/>";
                    html += "<input type=\"submit\" id=\"btn\"/>";
                html += "</form>";
                html += "<script>";
                    html += "document.querySelector(\"#btn\")";
                    html += ".addEventListener(\"click\", function(){";
                        html += "JI.setSection(document.querySelector(\"#login\").value)";
                    html += "});";
                    html += "let login = JI.getSection();";
                    html += "if(login != null) {"+
                                "document.querySelector(\"#login\").value = login;"+
                                "//document.querySelector(\"#btn\").click()"+
                            "}";
                html += "</script>";
            html += "</body>";
        html += "</html>";
        return html;
    }

    private class JI {
        @JavascriptInterface
        public void setSection(String login) {
            SharedPreferences prefs = MainActivity.this.getSharedPreferences("test", 0);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("login", login);
            boolean b = editor.commit();
            Toast.makeText(MainActivity.this, b ? "Guardado" : "Erro", Toast.LENGTH_LONG).show();
        }

        @JavascriptInterface
        public String getSection() {
            SharedPreferences prefs = MainActivity.this.getSharedPreferences("test", 0);
            String result = prefs.getString("login", null);
            return result;
        }
    }
}

Follow the main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webview"/>
</LinearLayout>

As you can see, the code is very simple, but I think I got the general idea of what I said!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.