Reputation: 483
I am trying to load a web page with a Facebook comment section into my Android WebView. I followed the solution provided by this question: Android unable to implement facebook comment in a webview due to default browser
When the user clicks the button in the web page to log into Facebook, they are presented with the login screen and can log in successfully, then that view closes and they are again presented with the original web page. However, they still aren't logged in. The original WebView is in the same state as before, and the login button is still here. What am I missing to actually get the login from the childView back to the original comment section? Relevant code is pasted below:
webView.setLayoutParams(getLayoutParams());
webView.setWebChromeClient(new DollyChromeClient());
webView.setWebViewClient(new DollyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setDisplayZoomControls(true);
webView.getSettings().setBuiltInZoomControls(true);
CookieManager.getInstance().setAcceptCookie(true);
private RelativeLayout.LayoutParams getLayoutParams(){
return new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
final class DollyChromeClient extends WebChromeClient {
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
childView = new WebView(mContext);
childView.getSettings().setDomStorageEnabled(true);
childView.getSettings().setJavaScriptEnabled(true);
childView.getSettings().setSupportZoom(true);
childView.getSettings().setBuiltInZoomControls(true);
childView.setWebViewClient(new FaceBookClient());
childView.setWebChromeClient(this);
childView.setLayoutParams(getLayoutParams());
childView.getSettings().setSupportMultipleWindows(true);
parentLayout.addView(childView);
childView.requestFocus();
webView.setVisibility(View.GONE);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(childView);
resultMsg.sendToTarget();
return true;
}
@Override
public boolean onConsoleMessage(ConsoleMessage cm) {
Log.d("WebViewDebug", cm.message() + " -- From line "
+ cm.lineNumber() + " of "
+ cm.sourceId());
return true;
}
@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;
}
}
Upvotes: 1
Views: 1903
Reputation: 1863
I have an example of a webview that works with facebook comments here, maybe it can be useful ... https://gist.github.com/nguyenkims/28f3dc9d2414c3184263fab52b93351e
Upvotes: 1
Reputation: 483
After days of pulling my hair out, this turned out to be caused by changes in Android Lollipop. It took finally testing on an older device to realize the source of the issue, which quickly led me to this: https://developer.android.com/about/versions/android-5.0-changes.html#BehaviorWebView
The ultimate solution turned out to be the following snippet of code:
if (Build.VERSION.SDK_INT >= 21) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
}
I hope this can help someone in the future pull out slightly less hair than I did over the last week.
Upvotes: 16