我正在开发一个包含iOS和Android本机应用程序的webapp.只需在两个平台的webView中显示webapp即可完成此操作.我遇到了Facebook登录和Android平台的问题.
登录到Facebook可以通过我的“黑客”工作但是当应用程序在之后检查登录状态时它总是返回未知(这表明SDK不知道用户是否登录到Facebook).
webView:
WebView mainWebView = (WebView) findViewById(R.id.petpulseWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.clearCache(true);
Javascript代码:
// 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×tamp=';
});
// 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等效项.
是否有一些设置或选项会阻止它在Android中运行?
最佳答案
我是对的,这是由于第三方cookie.
添加这些行修复了问题:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(mainWebView, true);
}
根据文件:
Apps that target KITKAT or below default to allowing third party cookies. Apps targeting LOLLIPOP or later default to disallowing third party cookies.
这意味着对于低于Lollipop的版本不需要此修复.
相关文章
- facebook - 即使用户登录,FB.getLoginStatus也会返回“未知”状态
- 如何检查Android应用程序中的Facebook登录状态.
- javascript - FB.getLoginStatus返回状态未知
- Facebook API:使用JavaScript SDK登录,然后使用PHP检查登录状态
- if(session.isOpen()),android登录android总是返回false
- Android登录Android WebView
- 尝试使用Facebook JS SDK从Facebook注销时,getLoginStatus返回状态未知
- 是否可以在iPhone的设置中检测Facebook登录状态?