Facebook喜欢和评论在Android Webview上不起作用 [英] Facebook like and comment not working on Android webview

查看:99
本文介绍了Facebook喜欢和评论在Android Webview上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试使用Android Studio和FB共享进行Webview,其他社交媒体共享按钮也可以正常工作.但是我网站上的FB直接喜欢和评论按钮不起作用.当我按类似按钮时,我可以看到白屏,其左上屏幕编号为"1".请帮我.

I have tried webview with Android studio and FB share and other social media share buttons are working fine. But FB direct like and comment button on my website is not working. When I press like button I can see white screen with left top screen number "1". Please kindly help me.

package com.example.neermaicom;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static android.content.Intent.*;

public class MainActivity extends AppCompatActivity {

private WebView webView;

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

getSupportActionBar().hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;

    try {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    } catch (Exception e) {

        return true;
    }
}
});
webView.loadUrl("http://www.neermai.com");

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
}

//This method require to use back button if want to go previous web page

 @Override
 public void onBackPressed() {

 if(webView.canGoBack()){
    webView.goBack();
 }else {
    super.onBackPressed();
 }
 }

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

推荐答案

这是由于Facebook用户auth,不幸的是android webview与auth系统并不完全兼容,我在webview应用程序中使用的另一种方法是使用" Chrome自定义标签以验证对我所评论系统的访问权限使用.

This is due to the Facebook user auth, unfortunately android webview is not fully compatible with auth systems, an alternative that I use in my webview applications is to use "Chrome Custom Tabs" to auth access to the system of comments that I use.

在我的应用程序中,当用户单击评论时,会在我的应用程序内打开一个Google Chrome标签,显示评论页面;当用户未登录时,Chrome会对该用户进行身份验证,而无需关闭或最小化我的应用程序来打开google铬合金.一切都在我自己的应用程序中完成.

In my application when the user clicks on comment, a Google Chrome tab is opened inside my application showing the comments page, when the user is not logged, the chrome auth the user without having to close or minimize my application to open google chrome. Everything is done within my own application.

要在我向ShouldOverrideUrlLoading添加"Intent"时启动Chrome标签,下面是我的代码示例.

To start the Chrome Tab when I added an "Intent" to shouldOverrideUrlLoading, below is an example of my code.

 @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if  (url.contains("#comments")){
        Uri uri = Uri.parse(url);
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        builder.build().launchUrl(PostActivity.this, uri);
        return true;
        }
        else {
        view.loadUrl(url);
        return true;
        }
    }

在Gradle应用中:

And in Gradle app:

implementation 'androidx.browser:browser:1.3.0-alpha01'

我的评论页面URL具有"#comments",您将其更改为您的评论页面URL中具有的标准,或者如果Facebook评论系统直接位于帖子页面上,则将标准的内容置于您的帖子的URL中在谷歌浏览器标签页中打开.

My url for comment pages has "#comments" you change to something standard that you have in your comment page URLs or if the facebook comment system is directly on the post page, put something that is standard in the url of your posts to open in the google chrome tab.

这篇关于Facebook喜欢和评论在Android Webview上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆