在webView中检查Facebook登录状态始终返回未知 [英] Checking Facebook login state in a webView always returns unknown

查看:128
本文介绍了在webView中检查Facebook登录状态始终返回未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,该应用程序包装在适用于iOS和Android的本机应用程序中.只需在两个平台的webView中显示webapp即可完成此操作.我在使用Facebook登录名和Android平台时遇到问题.

I am developing a webapp that is wrapped in a native app for both iOS and Android. This is done by simply displaying the webapp in a webView for both platforms. I am having issues with the Facebook login and the Android platform.

通过我的"hack"登录Facebook是可行的,但是当应用程序之后检查登录状态时,它始终返回未知(这表明SDK不知道用户是否已登录Facebook).

Logging in to Facebook works via my "hack" but when the application is checking the login state afterwards it always returns unknown (which indicates that the SDK does not know if the user is logged into Facebook or not).

webView:

WebView mainWebView = (WebView) findViewById(R.id.petpulseWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.clearCache(true);

JavaScript代码:

The Javascript code:

// This function works, user is redirected to the Facebook login like he/she should
$(document).on('click', '#loginfb', function (e) {
    e.preventDefault();
    $.mobile.loading('show');

    // Worst hack.ever
    window.location.href = 'https://m.facebook.com/v2.2/dialog/oauth?client_id=xxxxx&response_type=code&redirect_uri=' + encodeURIComponent($('#base_url').val() + 'index.php?id=4') + '&scope=email&timestamp=';
});

// Checking if both the Facebook SDK and the correct page is loaded, if the user has code in GET he/she has been redirected from a Facebook login
function check_all_loaded () {
    if (loaded_page && loaded_facebook) {
        if (document.URL.indexOf('code=') >= 0) {
            FB.getLoginStatus(function(response) {
                // This always returns "unknown"
                console.log(response.status);
            });
        }
    }
}

经过广泛测试,我被认为是某种政策或Android webView中的某些内容阻止"了Facebook检查登录状态的请求.此代码适用于所有台式机浏览器及其等效的iOS.

After extensive testing I am lead to belive this is due to some sort of policy or something in the Android webView that "blocks" the request Facebook does to check the login state. This code works for all desktop browser and its iOS equivalent.

是否存在某些设置或选项会阻止其像Android中那样工作?

Is there some setting or option that would block this from working like it should in Android?

推荐答案

我是对的,这是由于第三方Cookie造成的.

I was right, it was due to third party cookies.

添加以下行可解决问题:

Adding these lines fixed the problem:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    CookieManager.getInstance().setAcceptThirdPartyCookies(mainWebView, true);
}

根据文档:

定位为KITKAT或以下的应用程序默认允许第三方Cookie.定位到LOLLIPOP或更高版本的应用默认情况下不允许第三方Cookie.

Apps that target KITKAT or below default to allowing third party cookies. Apps targeting LOLLIPOP or later default to disallowing third party cookies.

这意味着对于低于Lollipop的版本不需要此修复程序.

Which means that for versions lower than Lollipop does not need this fix.

这篇关于在webView中检查Facebook登录状态始终返回未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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