Android WebView for Facebook Like Button

后端 未结 4 722
孤街浪徒
孤街浪徒 2020-11-29 23:43

I\'m trying to make facebook like functionality in Android WebView (project specification does not allow browser opening, or any out of application activity).

相关标签:
4条回答
  • 2020-11-29 23:53

    Didn't worked for me:(, but form observing i perceive that wrong redirected link started with

    url.startsWith("http://m.facebook.com/a/profile.php?fan&id"))
    
    0 讨论(0)
  • 2020-11-29 23:57

    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);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 00:05

    I had the same issue on my android application. The cause of the issue is FB login javascript opens a new page on a new window. 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. 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. A new window is created and inserted to the frame layout and removed upon success. I added the removal at shouldOverrideUrlLoading.

    0 讨论(0)
  • 2020-11-30 00:10

    I had to work through this almost exact same problem on 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.

    0 讨论(0)
提交回复
热议问题