简体   繁体   English

适用于Facebook的Android WebView按钮

[英]Android WebView for Facebook Like Button

I'm trying to make facebook like functionality in Android WebView (project specification does not allow browser opening, or any out of application activity). 我正在尝试在Android WebView中使用类似Facebook的功能(项目规范不允许浏览器打开,或任何不在应用程序活动中)。

So, restrictions are that it has to be done in WebView. 因此,限制是它必须在WebView中完成。 I've managed to make it a dialog, and apon user's click like button, it (the WebView) redirects successfully (in the same view) to facebooks login page. 我已经设法使它成为一个对话框,并且用户点击按钮,它(WebView)成功(在同一视图中)重定向到facebooks登录页面。 After successful authentication, the WebView (in a dialog) is redirected to blank page with facebook header. 身份验证成功后, WebView (在对话框中)将重定向到带有标题的空白页面

Interestingly enough, when user leaves the blank dialog and click again on the like button it works like perfectly (like and unlike) - it somehow keeps authentication active. 有趣的是,当用户离开空白对话框并再次单击类似按钮时,它的工作方式非常完美(喜欢和不同) - 它以某种方式保持身份验证活动。 To resolve the blank page, I've tried/used following: 要解决空白页面,我尝试过/使用过以下内容:

  • using WebViewClient and shouldOverloadUrlForwarding to keep whole process in same WebView dialog. 使用WebViewClientshouldOverloadUrlForwarding将整个进程保持在同一个WebView对话框中。
  • using WebChromeClient to properly execute JavaScript - without it after login is not possible to like/unlike. 使用WebChromeClient正确执行JavaScript - 在登录后没有它可能不喜欢/不同。
  • tried using setUserAgentString() to simulate other browsers like Chrome or Firefox 尝试使用setUserAgentString()来模拟Chrome或Firefox等其他浏览器
  • tried the SSL Error certificate handling (in API level 8) (at WebViewClient ) 尝试了SSL错误证书处理(在API级别8)(在WebViewClient

    @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); }

  • using (and all possible combination of these) 使用(以及所有可能的组合)

    webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

  • Tried also persisting cookies with CookieSyncManager , CookieManager and manually handling. 尝试使用CookieSyncManagerCookieManager持久存储cookie并手动处理。

All of this was with no result. 所有这一切都没有结果。 I really appreciate any help! 我非常感谢任何帮助!

To get past the blank page you do this: 要通过空白页面,请执行以下操作:

 webview.setWebViewClient(new LikeWebviewClient(this));

 private class LikeWebviewClient extends WebViewClient {        
    @Override
    public void onPageFinished(WebView view, String url) {
        Log.d(TAG, "onPageFinished url: " +url);
        // Facebook redirects to this url once a user has logged in, this is a blank page so we override this
        // http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php?............
        if(url.startsWith("http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php")){
            String redirectUrl = getFacebookLikeUrl();
            view.loadUrl(redirectUrl);
            return;
        }
        super.onPageFinished(view, url);
    }
}

I had the same issue on my android application. 我在Android应用程序上遇到了同样的问题。 The cause of the issue is FB login javascript opens a new page on a new window. 问题的原因是FB登录javascript在新窗口中打开一个新页面。 Then it tries to close it after login success. 然后它会在登录成功后尝试关闭它。 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. ID“webview”的Webview是我内容的主视图。 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 . 这个问题的关键是onCreateWindow A new window is created and inserted to the frame layout and removed upon success. 创建一个新窗口并将其插入到框架布局中,并在成功时将其删除。 I added the removal at shouldOverrideUrlLoading . 我在shouldOverrideUrlLoading添加了删除。

没有为我工作:(,但形式观察我认为错误的重定向链接开始

url.startsWith("http://m.facebook.com/a/profile.php?fan&id"))

I had to work through this almost exact same problem on iPhone. 我不得不在iPhone上解决这个几乎完全相同的问题。 What I had to do was to intercept the request that the webview makes to the 'blank page' you described above, and instead tell the webview to load the like URL. 我必须做的是拦截webview对你上面描述的“空白页面”的请求,而是告诉webview加载类似的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM