Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView HTTP Cookies not working in API 21

I have an Android application that uses WebView and HTTP cookies. This application works on Android devices running API 19 or below. API 21 is not saving the http cookie for later reference.

Android WebView Code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_token);
    WebView mWebView = (WebView) findViewById(R.id.activity_main_webView1);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.setWebChromeClient(new WebChromeClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            return false;
        }
    });
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setGeolocationEnabled(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.getSettings().setDatabaseEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    mWebView.loadUrl("file:///android_asset/index.html");
}

Android Manifest

uses-sdk android:minSdkVersion="16" android:targetSdkVersion="19"
uses-permission android:name="android.permission.INTERNET"

Server Side Code creating cookie:

Response.Cookies("mycookie")("myvalue") = "123456789"
Response.Cookies("mycookie").Expires = Date() + 10
Response.Cookies("mycookie").Secure = True

Server Side Code reading cookie:

Response.Write Request.Cookies("mycookie")("myvalue")
  • This returns a blank value on API 21 in WebView

When this runs on API 19 or below I can read/write cookies no problem. I am using cookies as you would with visiting any web page that uses cookies. Any help would be appreciated.

like image 798
Zeb Avatar asked Feb 09 '15 20:02

Zeb


People also ask

Does WebView save cookies?

By doing so, whenever a cookie is set by the API through the API call using the particular instance of okHttpClient , the cookie will be stored automatically and will be used by Webview launched by the App.

How do I clear cookies on WebView Android?

Open your browser. Android browser: Go to Menu > More > Settings or Menu > Settings > Privacy & Security. Chrome: Go to Menu > Settings > Privacy. Android browser: Tap Clear cache, Clear history, and Clear all cookie data as appropriate.

What is cookie manager android?

android.webkit.CookieManager. Manages the cookies used by an application's WebView instances. CookieManager represents cookies as strings in the same format as the HTTP Cookie and Set-Cookie header fields (defined in RFC6265bis).


1 Answers

API 21 or Lollipop requires this to be added to your APP

if (Build.VERSION.SDK_INT >= 21) {
    // AppRTC requires third party cookies to work
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptThirdPartyCookies(mWebView, true);
}

Works again!

like image 168
Zeb Avatar answered Oct 10 '22 03:10

Zeb