Question

I'm trying to use a facebook login button that uses JS Facebook SDK on Android Webview. When I click it open a new page and redirects to https://www.facebook.com/dialog/oauth... which is a blank page with a javascript code. And the webview stays here.

I'm using:

webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Thank you!

No correct solution

OTHER TIPS

You need to add logic to redirect back to the original url of your website.

In order to do this, you need to first create a new java class that extends the WebViewClient class and overrides the onPageFinished method like this:

public class CustomWebViewClient extends WebViewClient
{      
        @Override
        public void onPageFinished(WebView view, String url) {

            //https://www.facebook.com/dialog/permissions.request  
            //actually  works for me, but I put the URL you say is coming up    
            //blank in there instead, whatever works for you:
            if(url.startsWith("https://www.facebook.com/dialog/oauth")){
               String redirectUrl = "http://www.mydomain.com/MyApp/";
                view.loadUrl(redirectUrl);
               return;
           }
            super.onPageFinished(view, url);
        }
}

Second, just add it to your WebView:

webview.setWebViewClient(new CustomWebViewClient());

Once that page is finished loading, it will redirect back to your original page

I dont know what exactly i did at that time, but the problem was solved. I'll give you the code. Try finding out :)

WebView browser,mWebviewPop;
private void open(){
    browser = (WebView)findViewById(R.id.webView1);
    browser.getSettings().setLoadsImagesAutomatically(true);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setAppCacheEnabled(true);
    browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    browser.getSettings().setSupportMultipleWindows(true);
    browser.setWebViewClient(new MyBrowser());
    browser.setWebChromeClient(new MyCustomChromeClient());
    mContext=this.getApplicationContext();
    browser.loadUrl(target_url);
    MainActivity.this.progressBar.setProgress(0);
    browser.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
        }
    });
}

private class MyBrowser extends WebViewClient {
    String redirectUrl = "MY URL";

    private void noInternet() {
        WebView webview = (WebView) findViewById(R.id.webView1);
        RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
        RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
        tryAgainLayout.setVisibility(View.VISIBLE);
        webview.setVisibility(View.GONE);
        webview.destroy();
        progressLayout.setVisibility(View.INVISIBLE);
    }


    public void visible(){
        WebView webview = (WebView) findViewById(R.id.webView1);
        RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
        RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
        tryAgainLayout.setVisibility(View.INVISIBLE);
        webview.setVisibility(View.INVISIBLE);
        progressLayout.setVisibility(View.VISIBLE);
    }

    public void unvisible(){
        WebView webview = (WebView) findViewById(R.id.webView1);
        RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
        RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
        tryAgainLayout.setVisibility(View.INVISIBLE);
        webview.setVisibility(View.VISIBLE);
        progressLayout.setVisibility(View.INVISIBLE);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String host = Uri.parse(url).getHost();
        if (host.equals(target_url_prefix))
        {
            if(mWebviewPop!=null)
            {
                mWebviewPop.setVisibility(View.GONE);
                baseLayout.removeView(mWebviewPop);
                mWebviewPop=null;
            }
            return false;
        }

        if(host.equals("m.facebook.com"))
        {
            return false;
        }
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode,
                                String description, String failingUrl) {
        noInternet();
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        visible();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        unvisible();
        System.out.println("\n" +view.getUrl());
        if(url.startsWith("https://m.facebook.com/v2.1/dialog/oauth")){
            if(mWebviewPop!=null)
            {
                mWebviewPop.setVisibility(View.GONE);
                baseLayout.removeView(mWebviewPop);
                mWebviewPop=null;
            }
            view.loadUrl(redirectUrl);
            return;
        }
        super.onPageFinished(view, url);
    }
}

private class MyCustomChromeClient extends WebChromeClient
{

    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog,
                                  boolean isUserGesture, Message resultMsg) {
        mWebviewPop = new WebView(mContext);
        mWebviewPop.setVerticalScrollBarEnabled(false);
        mWebviewPop.setHorizontalScrollBarEnabled(false);
        mWebviewPop.setWebViewClient(new MyBrowser());
        mWebviewPop.getSettings().setJavaScriptEnabled(true);
        mWebviewPop.getSettings().setSavePassword(false);
        mWebviewPop.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        baseLayout.addView(mWebviewPop);
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(mWebviewPop);
        resultMsg.sendToTarget();

        return true;
    }

    @Override
    public void onCloseWindow(WebView window) {
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        MainActivity.this.setValue(newProgress);
        super.onProgressChanged(view, newProgress);
    }
}

OK so from what I can tell, what's happening here is that there are two "ways" for oauth to work, either it will call back "to aparent page" (like popup "log me in" that disappears), or it can redirect you to a login page, then to some other url (non popup style), after success or failure (kind of a chain forward). Unfortunately it appears WebView is not well suited for the "callback to the parent page" style, so you have to do some shenanigans like have a second WebView (jincy's answer here, or see also this).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top