2

我正在尝试使用在 Android Webview 上使用 JS Facebook SDK 的 facebook 登录按钮。当我点击它打开一个新页面并重定向到https://www.facebook.com/dialog/oauth ... 这是一个带有 javascript 代码的空白页面。网络视图留在这里。

我在用着:

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

谢谢!

4

3 回答 3

3

您需要添加逻辑以重定向回您网站的原始网址。

为此,您需要首先创建一个扩展WebViewClient类并覆盖onPageFinished方法的新 java 类,如下所示:

public class CustomWebViewClient extends WebViewClient
{      
        @Override
        public void onPageFinished(WebView view, String url) {

            //https://www.facebook.com/dialog/permissions.request  
            //actually  works for me, but I put the URL you say is coming up    
            //blank in there instead, whatever works for you:
            if(url.startsWith("https://www.facebook.com/dialog/oauth")){
               String redirectUrl = "http://www.mydomain.com/MyApp/";
                view.loadUrl(redirectUrl);
               return;
           }
            super.onPageFinished(view, url);
        }
}

其次,只需将其添加到您的WebView

webview.setWebViewClient(new CustomWebViewClient());

该页面完成加载后,它将重定向回您的原始页面

于 2012-11-01T13:23:59.250 回答
2

我不知道我当时到底做了什么,但问题已经解决了。我给你代码。尝试找出:)

WebView browser,mWebviewPop;
private void open(){
    browser = (WebView)findViewById(R.id.webView1);
    browser.getSettings().setLoadsImagesAutomatically(true);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setAppCacheEnabled(true);
    browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    browser.getSettings().setSupportMultipleWindows(true);
    browser.setWebViewClient(new MyBrowser());
    browser.setWebChromeClient(new MyCustomChromeClient());
    mContext=this.getApplicationContext();
    browser.loadUrl(target_url);
    MainActivity.this.progressBar.setProgress(0);
    browser.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
        }
    });
}

private class MyBrowser extends WebViewClient {
    String redirectUrl = "MY URL";

    private void noInternet() {
        WebView webview = (WebView) findViewById(R.id.webView1);
        RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
        RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
        tryAgainLayout.setVisibility(View.VISIBLE);
        webview.setVisibility(View.GONE);
        webview.destroy();
        progressLayout.setVisibility(View.INVISIBLE);
    }


    public void visible(){
        WebView webview = (WebView) findViewById(R.id.webView1);
        RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
        RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
        tryAgainLayout.setVisibility(View.INVISIBLE);
        webview.setVisibility(View.INVISIBLE);
        progressLayout.setVisibility(View.VISIBLE);
    }

    public void unvisible(){
        WebView webview = (WebView) findViewById(R.id.webView1);
        RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
        RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
        tryAgainLayout.setVisibility(View.INVISIBLE);
        webview.setVisibility(View.VISIBLE);
        progressLayout.setVisibility(View.INVISIBLE);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String host = Uri.parse(url).getHost();
        if (host.equals(target_url_prefix))
        {
            if(mWebviewPop!=null)
            {
                mWebviewPop.setVisibility(View.GONE);
                baseLayout.removeView(mWebviewPop);
                mWebviewPop=null;
            }
            return false;
        }

        if(host.equals("m.facebook.com"))
        {
            return false;
        }
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode,
                                String description, String failingUrl) {
        noInternet();
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        visible();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        unvisible();
        System.out.println("\n" +view.getUrl());
        if(url.startsWith("https://m.facebook.com/v2.1/dialog/oauth")){
            if(mWebviewPop!=null)
            {
                mWebviewPop.setVisibility(View.GONE);
                baseLayout.removeView(mWebviewPop);
                mWebviewPop=null;
            }
            view.loadUrl(redirectUrl);
            return;
        }
        super.onPageFinished(view, url);
    }
}

private class MyCustomChromeClient 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 MyBrowser());
        mWebviewPop.getSettings().setJavaScriptEnabled(true);
        mWebviewPop.getSettings().setSavePassword(false);
        mWebviewPop.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        baseLayout.addView(mWebviewPop);
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(mWebviewPop);
        resultMsg.sendToTarget();

        return true;
    }

    @Override
    public void onCloseWindow(WebView window) {
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        MainActivity.this.setValue(newProgress);
        super.onProgressChanged(view, newProgress);
    }
}
于 2015-11-17T09:05:14.283 回答
0

好的,据我所知,这里发生的事情是 oauth 有两种“方式”可以工作,或者它会回调“到父页面”(如弹出的“登录我”消失),或者它可以重定向你到一个登录页面,然后到其他一些 url(非弹出式样式),在成功或失败之后(一种链式转发)。不幸的是,WebView 似乎不太适合“回调父页面”样式,因此您必须做一些恶作剧,例如拥有第二个 WebView(此处为 jincy 的答案,或另见此

于 2017-10-11T06:43:05.083 回答