How to enable cookies for Android phonegap 1.8.0 app?

后端 未结 3 707
臣服心动
臣服心动 2020-12-15 14:43

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 th

相关标签:
3条回答
  • 2020-12-15 15:24

    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");
    }
    }
    
    0 讨论(0)
  • 2020-12-15 15:34

    you actually wanna do

    import android.webkit.CookieManager;
    import org.apache.cordova.*;
    
    public class MainActivity extends DroidGap {
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
          try{
              CookieManager.setAcceptFileSchemeCookies(true);
          }catch(Throwable e){}
    

    Since the CookieManager does not exist on older Android versions

    0 讨论(0)
  • 2020-12-15 15:39

    If my memories are good, the Phonegape's template loads your start-up web page (ex: index.html) by loading it from the webview (something like : super.loadUrl("file:///android_asset/www/index.html");). The latter is declared in the main activity (a ".java" file).

    So, first, find the instruction that loads your web application. Then, add the following lines before the webview.loadUrl so that you can obtain something like this :

    CookieSyncManager.createInstance(this);
    CookieSyncManager.getInstance().startSync();
    webView = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    super.loadUrl("file:///android_asset/www/index.html");
    

    Finally, refresh the Android's project and relunch the app.

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