How to enable cookies for Android phonegap 1.8.0 app?

android, cookies, cordova

Solution

If you are trying to use local cookies (file://) you have to make the parent Phonegap project accept local cookies. To do so, You should have a file called youappname.java in your PhoneGap project, probably with this contents or similar:

import android.os.Bundle;
import org.apache.cordova.*;

public class App extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/index.html");
}
}

Modify it to look like this example:

import android.os.Bundle;
import android.webkit.CookieManager;
import org.apache.cordova.*;

public class App extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    CookieManager.setAcceptFileSchemeCookies(true);
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/index.html");
}
}

Problem

I understood that in order to use cookies inside of phonegap native app there must be piece of code which enables it. When building phonegap for iOS using xcode 4 there is such piece of code inside of phonegap template. Could you please advice me which code and where I need to put in order to enable cookies for Android phonegap 1.8.0 app? Please note that I'm using the eclipse Indigo 3.7.2 for building of the app. Many thanks. Cheers, Sinisa.

Original source