Android cannot implement facebook comment in webview due to default browser

I'm new to Android development, and this webview and webview client are killing me. This is my scenario:

  • I need to load a webpage containing the social facebook plugin (used to comment on this particular url) and I use WebView for it.
  • When a user clicks on a comment via Facebook, he / she should be presented with the login page for the same web view (instead of opening the default browser).
  • And as soon as the login is successful, the first page will be displayed (the one that contains the social plugin), allowing the user to comment

What I need to do is emulate the browser workflow, that is, the user, when logged in, is automatically granted permission to add comments to facebook.

My problem:

I do not know how to get all authentication from the browser and redirect it back to my application. I want the whole process to run in my application web browser, and not in the default browser.

I checked all the questions and most of them are advised to use the open source Facebook plugins like Facebook connect and Facebook android SDK. Next, I got information about CookieManager , CookieSyncManager , WebViewClient , WebChromeClient , but I could not implement my problem. And the closest I found the following:

How to handle facebook as a confirmation in the Android web browser.

So, if you could point me in the right direction, I would be very happy. I'm still trying to figure out how to do all the actions in the webview, and if anything comes up, I will definitely post.

Thanks in advance

Update

I could only implement facebook login, but could not implement AOL , Hotmail and Yahoo login. To login facebook just create a custom WebViewClient and by the method shouldOverrideUrlLoading

 if(url.contains("https://www.facebook.com/connect/window_comm.php")){ webView.clearHistory(); webView.loadUrl(remoteUrl); } return false; 

To allow multiple logins, I did the following technique, but it doesn’t work

  if(url.contains("https://www.facebook.com/connect/window_comm.php")){ String cookieString = cookieManager.getCookie("facebook.com"); if(cookieString != null){ cookieManager.setCookie("remoteUrldomain.com", cookieString); CookieSyncManager.getInstance().sync(); webView.clearHistory(); webView.loadUrl(remoteUrl); } } return false; 

I am still doing my best to find a solution, and anyone who is there will lead me in the right direction, which would be grateful. thanks in advance

+7
source share
1 answer

The answer to How to handle facebook as a confirmation in the Android web browser is the best solution I have found so far. What I found out is that Android WebView does not load html views popup by default, and for this we need to use WebChromeClient , which handles all of these. Look at the following code

  private String requestUrl="some web link containing facebook social comment"; private WebView webView,childView =null; private LinearLayout parentLayout; private Activity MyActivity; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); this.getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON); parentLayout =(LinearLayout)findViewById(R.id.parentLayout); MyActivity = this; webView = new WebView(this); webView.setLayoutParams(getLayoutParams()); webView.setWebViewClient(new FaceBookClient()); webView.setWebChromeClient(new MyChromeClient()); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); webView.getSettings().setSupportMultipleWindows(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setBuiltInZoomControls(true); parentLayout.addView(webView); webView.loadUrl(requestUrl); } private LinearLayout.LayoutParams getLayoutParams(){ return new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); } final class MyChromeClient extends WebChromeClient{ @Override public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) { childView = new WebView(SmCommentTestActivity.this); childView.getSettings().setJavaScriptEnabled(true); childView.getSettings().setSupportZoom(true); childView.getSettings().setBuiltInZoomControls(true); childView.setWebViewClient(new FaceBookClient()); childView.setWebChromeClient(this); childView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); parentLayout.addView(childView); childView.requestFocus(); webView.setVisibility(View.GONE); /*I think this is the main part which handles all the log in session*/ WebView.WebViewTransport transport =(WebView.WebViewTransport)resultMsg.obj; transport.setWebView(childView); resultMsg.sendToTarget(); return true; } @Override public void onProgressChanged(WebView view, int newProgress) { MyActivity.setProgress(newProgress*100); } @Override public void onCloseWindow(WebView window) { parentLayout.removeViewAt(parentLayout.getChildCount()-1); childView =null; webView.setVisibility(View.VISIBLE); webView.requestFocus(); } } private class FaceBookClient extends WebViewClient{ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i("REQUEST URL",url); return false; } } @Override public void onBackPressed() { if(childView != null && parentLayout.getChildCount()==2){ childView.stopLoading(); parentLayout.removeViewAt(parentLayout.getChildCount()-1); if(webView.getVisibility() == View.GONE) webView.setVisibility(View.VISIBLE); }else{ super.onBackPressed(); } } 

That’s all you need to do to implement Facebook Social Comment Plugin on Android WebView , and if someone finds any flaw in it, I would be happy to fix it. And I hope that this solution will save time on any problematic developer like me;)

+11
source

All Articles