简体   繁体   中英

Close activity after successful user login (WebView)

I'm making an Android mobile application which uses 3rd party login page for authentication loaded in WebView. When user enters username/pass and submits the form it saves cookie on user's device and redirects user to the next page.

What I want to do here is to prevent that redirection, close login activity after the cookie has been saved to user's device and display main activity to the user.

Any advice? Thanks.

Thanks for quick answers. I figured out solution that worked for me.

As I explained in the question, after user enters credentials and successfully login next page is loaded in WebView. What I did is to catch that redirection and close the activity. Something like this:

myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("https://www.someservice/nextpage")) {
            Intent i = new Intent(LogInActivity.this,MainActivity.class);
            startActivity(i);
            finish();
        }
        return true;
    }
}

I'm not sure if this is the most elegant solution but it's short and it work for me :)

Use a seperate activity for WebView..

@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
    if (url.contains("http://redirectingurl")
        finish();  // close activity
    else
        view.loadUrl(url);

    return true; 
}

I didnt try, but i think this will work. For refernce

I'm not able to post all codes right now but I'll give you the idea.

in onPageFinished(Webview view, STring url) method you can inject a javascript file that controls login button in that 3rd party web page, something like this:

@Override
public void onPageFinished(WebView view, String url) {
    view.loadUrl("javascript:/injection.js content will be here/")

}

injection.js

document.getElementById("loginButton").addEventListener("click", function(){
    //login process copy paste without redirection
    loginMethod()
});

And add a JavaScriptInterface to webView:

mWebView.addJavascriptInterface(new JsInterfaceLogin(this.getContext()), "MY_APP");

JsInterfaceLogin:

public class JsInterfaceLogin {

    private final Context context;

    public JsInterfaceLogin(Context context) {
       this.context = context;
    }

    @JavascriptInterface
    public void ___loggedIn___(String toast) {
       Toast.makeText(context, ""+toast, Toast.LENGTH_SHORT).show();
       //When user logged in, you can detect it in here
    }
}

And in loginMethod() function in injection.js, call this after login process:

window.MY_APP.___loggedIn___("test")

After successful user login will close login activity and start a new activity

It is working Smoothly.... and I applied.

package com.loginsql.afterloginmove;


import android.content.Intent;
import android.os.Bundle;

import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {


    private WebView webViewLogin;
    private TextView loginTextView;


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


        webViewLogin = findViewById(R.id.myWebView);
        loginTextView = findViewById(R.id.logintext);


        webViewLogin.setWebViewClient(new WebViewClient());
        webViewLogin.setWebChromeClient(new WebChromeClient());

        webViewLogin.getSettings().setJavaScriptEnabled(true);

        webViewLogin.loadUrl("https://myweblogin.com/auth/index");

        webViewLogin.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains(url)) {
                    Intent i = new Intent(MainActivity.this, DashboardActivityActivity.class);
                    startActivity(i);
                    finish();

                }
                return true;
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM