Search code examples
androidcordovaphonegap-pluginschildbrowser

Adding CookieManager to android Childbrowser


I am trying to get local cookies (file://) to work on android 3.1+ within ChildBrowser. I found a blog response talking about this specific issue and how to remedy it with Cookie Manager. I can't figure out where in the plugin to put the code. Has anyone successfully implemented this?


Comment Below from http://code.google.com/p/android/issues/detail?id=3739

Comment 16 by [email protected], Feb 1, 2012 I got this thing working, for Android 2.2, javascript's document.cookie works fine, just make sure that in your Webview...javascript is enabled like so:

yourWebViewVariable.getSettings().setJavaScriptEnabled(true);

for Android 3.1 just add this to your java file onLoadInit:

CookieManager.setAcceptFileSchemeCookies(true);   //This is the line that specifically makes it work so the other lines is optional

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.acceptCookie();

Solution

  • For those interested, I figured it out. It is not a Childbrowser issue at all. You have to make the parent Phonegap project accept local cookies and then Childbrowser will too. 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");
    }
    }