Cordova session cookies don't work on Android Lollipop

前端 未结 1 558
梦如初夏
梦如初夏 2021-02-05 21:18

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

相关标签:
1条回答
  • 2021-02-05 21:34

    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/

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