Cordova会话Cookie不适用于Android Lollipop [英] Cordova session cookies don't work on Android Lollipop
问题描述
我开发了一个Android的Cordova / Phonegap应用程序,使用会话Cookie登录第三方网站。
但是,当我将智能手机更新到Android Lollipop 5.0时,还有应用程序库到API级别21,Cookie停止工作。有什么变化?
在互联网上查找一个有效的解决方案后,我遇到了一个解释问题的文章非常好,所以我把它发布在这里,因为我认为这将是有用的其他Stack Overflow用户。
基本上,问题在于新的Android第三方cookies政策( https://developer.android.com/about/versions/ android-5.0-changes.html#BehaviorWebView ),它会默认阻止它们。解决方案是在主活动中添加几行代码:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
//为Android Lollipop允许第三方Cookie
if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.LOLLIPOP){
WebView webView =(WebView)super。 appView;
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView,true);
}
super.loadUrl(Config.getStartUrl());
}
有关详细信息, href =http://joashpereira.com/blog/2014/11/19/fix-to-cordovaphonegap-apps-targeting-android-5-lollipop-or-later-on-default-disallowing-third-party-cookies /rel =nofollow> http://joashpereira.com/blog/2014/11/19/fix-to-cordovaphonegap-apps-targeting-android-5-lollipop-or-later-on-default-disallowing-第三方Cookie /
I develop a Cordova/Phonegap app for Android which uses session cookies to login to third party websites. For this, I do an AJAX post request (with jQuery) and then cookies are set automatically.
But, when I updated my smartphone to Android Lollipop 5.0 and also the app libraries to API level 21, cookies stopped working. What has changed?
After hours spent looking on the Internet for a working solution, I came across an article that explains the issue really well, so I am posting it here because I thought it will be useful to other Stack Overflow users.
Basically, the problem lies on the new Android third party cookies policy (https://developer.android.com/about/versions/android-5.0-changes.html#BehaviorWebView), which blocks them by default.
The solutions is to add a few lines of code to the main activity:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
// Allow third party cookies for Android Lollipop
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView webView = (WebView)super.appView;
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView,true);
}
super.loadUrl(Config.getStartUrl());
}
For more information, I put a link to the full article here: http://joashpereira.com/blog/2014/11/19/fix-to-cordovaphonegap-apps-targeting-android-5-lollipop-or-later-on-default-disallowing-third-party-cookies/
这篇关于Cordova会话Cookie不适用于Android Lollipop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!