Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making facebook login work with an Android Webview

I am just trying to implement the facebook login on a WebView in Android. The problem is after I click the facebook button on my HTML page and insert the username and password on Facebook dialog. The url redirect is just giving me a black page.

   @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      WebView webview = new WebView(this);             webview.setWebChromeClient(new WebChromeClient());       webview.getSettings().setPluginState(PluginState.ON);     webview.getSettings().setJavaScriptEnabled(true);      webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);     webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);     webview.setWebViewClient(new WebViewClient());     webview.loadUrl("http://peoplehunt.crowdscanner.com/hunt");      setContentView(webview); 

This is the Facebook regular javascript API on my HTML page and this function gets called when the facebook button is clicked.

$("#login_facebook").click(function() {                      FB.login(function(response) {                             //This function should be called                             if (response.authResponse) {                             FB.api('/me?fields=name,email,picture,id&type=large', function(response) {                                     //console.log("email "+response.email);                                     $("#submitHandle").hide();                                     $("#loader").show();                                     //console.log('Good to see you, ' + response.picture + '.');                                     var theUsername = response.name;                                     theUsername = theUsername.replace(/ /g, '_')+"_"+response.id+"@meetforeal.com";                                     //console.log(theUsername);                                     $("input[name=email]").val(encodeURIComponent(response.email));                                     $("input[name=lastName]").val(encodeURIComponent(response.name));                                     $("input[name=avatarImage]").val(response.picture);                                     $("input[name=userName]").val(encodeURIComponent(theUsername));                                     $("#msg_twitter").fadeIn("slow");                                     $("#submitHandle").show();                                     $("#loader").hide();                                     $("#user").attr("action","/crowdmodule/auth/registerattendeefacebook");                                     $("#user").submit();                             });                             } else {   //console.log('User cancelled login or did not fully authorize.');  } }, {scope: 'email'}); 

ANY ideas on how to get the response back after the redirect on the Facebook dialog page? THANKS.

like image 763
Adrian Avendano Avatar asked Sep 28 '12 22:09

Adrian Avendano


People also ask

Does Facebook use Android WebView?

Deprecating Facebook Login support on Android WebViewsBeginning October 5, 2021, Facebook Login will no longer support using Android embedded browsers (WebViews) for logging in users.

Is Facebook a WebView app?

Later today it's releasing Facebook for Android 2.0, which replaces the hybrid native/webview code with an all-native infrastructure to answer years of complaints by making the app much faster.


1 Answers

I had the same issue on my android application. The cause of the issue is FB login javascript opens a new page on a new window. Then it tries to close it and send some javascript auth codes back, after login success. WebView is typically "single window only" so it has no place to go back to, hence the blank screen.

Please follow flowing example from my working codes.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0099cc" tools:context=".MyActivity"  android:id="@+id/webview_frame">  <WebView     android:id="@+id/webview"     android:layout_width="fill_parent"     android:layout_height="fill_parent" /> 

The Webview of id "webview" is the main view for my content. Below is my activity codes.

public class MyActivity extends Activity {  /* URL saved to be loaded after fb login */ private static final String target_url="http://www.example.com"; private static final String target_url_prefix="www.example.com"; private Context mContext; private WebView mWebview; private WebView mWebviewPop; private FrameLayout mContainer; private long mLastBackPressTime = 0; private Toast mToast;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      setContentView(R.layout.activity_urimalo);     // final View controlsView =     // findViewById(R.id.fullscreen_content_controls);     CookieManager cookieManager = CookieManager.getInstance();      cookieManager.setAcceptCookie(true);      mWebview = (WebView) findViewById(R.id.webview);     //mWebviewPop = (WebView) findViewById(R.id.webviewPop);     mContainer = (FrameLayout) findViewById(R.id.webview_frame);     WebSettings webSettings = mWebview.getSettings();     webSettings.setJavaScriptEnabled(true);     webSettings.setAppCacheEnabled(true);     webSettings.setJavaScriptCanOpenWindowsAutomatically(true);     webSettings.setSupportMultipleWindows(true);     mWebview.setWebViewClient(new UriWebViewClient());     mWebview.setWebChromeClient(new UriChromeClient());     mWebview.loadUrl(target_url);      mContext=this.getApplicationContext();  }   private class UriWebViewClient extends WebViewClient {     @Override     public boolean shouldOverrideUrlLoading(WebView view, String url) {         String host = Uri.parse(url).getHost();         //Log.d("shouldOverrideUrlLoading", url);         if (host.equals(target_url_prefix))          {             // This is my web site, so do not override; let my WebView load             // the page             if(mWebviewPop!=null)             {                 mWebviewPop.setVisibility(View.GONE);                 mContainer.removeView(mWebviewPop);                 mWebviewPop=null;             }             return false;         }          if(host.equals("m.facebook.com"))         {             return false;         }         // Otherwise, the link is not for a page on my site, so launch         // another Activity that handles URLs         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));         startActivity(intent);         return true;     }      @Override     public void onReceivedSslError(WebView view, SslErrorHandler handler,             SslError error) {         Log.d("onReceivedSslError", "onReceivedSslError");         //super.onReceivedSslError(view, handler, error);     } }  class UriChromeClient 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 UriWebViewClient());         mWebviewPop.getSettings().setJavaScriptEnabled(true);         mWebviewPop.getSettings().setSavePassword(false);         mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,                 ViewGroup.LayoutParams.MATCH_PARENT));         mContainer.addView(mWebviewPop);         WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;         transport.setWebView(mWebviewPop);         resultMsg.sendToTarget();          return true;     }      @Override     public void onCloseWindow(WebView window) {         Log.d("onCloseWindow", "called");     }  } } 

The key for this issue is onCreateWindow. A new window is created and inserted to the frame layout and removed upon success. I added the removal at shouldOverrideUrlLoading.

like image 155
Ken You Avatar answered Oct 06 '22 20:10

Ken You