gpt4 book ai didi

android - Android Webview 中的 Facebook 评论

转载 作者:行者123 更新时间:2023-11-29 14:18:59 24 4
gpt4 key购买 nike

我正在尝试将带有 Facebook 评论部分的网页加载到我的 Android WebView 中。我遵循了这个问题提供的解决方案:Android unable to implement facebook comment in a webview due to default browser

当用户点击网页中的按钮登录 Facebook 时,他们会看到登录屏幕并可以成功登录,然后该 View 会关闭,他们会再次看到原始网页。但是,他们仍然没有登录。原来的WebView和之前一样的状态,登录按钮还在。我缺少什么才能真正从 childView 登录回到原来的评论部分?相关代码贴在下面:

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;
}
}

最佳答案

经过几天的努力,我发现这是由 Android Lollipop 的变化引起的。最终在一台旧设备上进行了测试,才意识到问题的根源,这很快让我想到了这个:https://developer.android.com/about/versions/android-5.0-changes.html#BehaviorWebView

最终的解决方案是下面的代码 fragment :

if (Build.VERSION.SDK_INT >= 21) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
}

我希望这可以帮助将来有人拔掉比我上周拔得少的头发。

关于android - Android Webview 中的 Facebook 评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30111196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com