0

I'm building my first android webview app for an existing website of mine and I'm having trouble with Facebook and Google login. In both cases I press the login button, the website loads on Chrome, I enter my details (say facebook) and then i get a blank page and that's it. At this point this webpage was supposed to close and send me back to the app but its not working.

The process is working fine from the browser on the same devices. Here is my code:

MainActivity.java

package com.myProject.www.myProject;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    WebView myProject;

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

    private void setPage(){
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        myProject = (WebView) findViewById(R.id.projViewId);
        myProject.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        WebSettings projSettings = myProject.getSettings();
        projSettings.setJavaScriptEnabled(true);
        projSettings.setDomStorageEnabled(true);
        projSettings.setAppCacheEnabled(true);
        projSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        projSettings.setSupportMultipleWindows(false);
        myProject.setWebChromeClient(new WebChromeClient() {

            @Override

            public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
                WebView.HitTestResult result = view.getHitTestResult();
                String data = result.getExtra();

                WebView newWebView = new WebView(view.getContext());
                WebView.WebViewTransport transport =
                        (WebView.WebViewTransport) resultMsg.obj;
                transport.setWebView(view);
                resultMsg.sendToTarget();

                return true;
            }
        });

        myProject.setWebViewClient(new MyAppWebViewClient(){
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                myProject.loadUrl("file:///android_asset/no-internet.html");
            }
        });
        myProject.loadUrl("https://www.myProject.com");
    }

    @Override
    public void onBackPressed() {
        if(myProject.canGoBack())
            myProject.goBack();
        else
            super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }
}

This is MyAppWebViewClient.java

package com.myProject.www.myProject;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyAppWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().endsWith("myProject.com")) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Whip
  • 1,891
  • 22
  • 43

2 Answers2

0

check your app's SHA-1 fingerprint

0

This is a pretty standard limitation of android webview, with third party authentication systems. This shoud help:

Making facebook login work with an Android Webview

You should also try chrome custom tabs instead. Here's how to use it with Facebook: https://developers.facebook.com/docs/facebook-login/android

Btw FB federated login system is among the most interesting I've encountered.

Community
  • 1
  • 1
Shubham Jain
  • 431
  • 3
  • 13