I've been reading answers, questions, forums and blogs for 2 days now but can't fix my issue.
I've an AngularJS webApp working in a Native (SDK) Android App with a WebView.
Everything seems to work but when my application calls services that responds with the "Set-Cookie" header those cookies are not setted in the webview and, of course are neither sent in the next calls, so my app explodes.
The app works flawlessly in browser and cordova and "document.cookie" returns an empty string, so i know this is where it fails.
My webview is initialized like this:
public class Main extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new mWebViewClient());
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setSavePassword(true);
webview.getSettings().setAllowFileAccessFromFileURLs(true);
webview.getSettings().setAllowContentAccess(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString(...);
webview.loadUrl("file:///android_asset/www/index.html");
javascriptAPI = new JavascriptAPI(this);
webview.addJavascriptInterface(javascriptAPI, "jsAPI");
...
}
...
}
I've tried also with chunks of code from here and there like:
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().run();
CookieManager.getInstance().setAcceptCookie(true);
But it doesn't work neither.
My app is calling to some services that use redirect, so fetching the cookies from JS and setting them is not an option, i need the webview to work like a browser would.
This is my first SDK Android App and i'm not sure at all what info could be relevant to answer the question (manifest, jsAPI, etc) so don't hessitate to say and i'll post whatever else is needed
SOLVED
Thank you @Mark, this fixed my code:
(inside Activity onCreate)
...
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
CookieSyncManager.createInstance(this);
}
^^^^^^^
THIS
WebView webview = (WebView) findViewById(R.id.webview);
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().run();
CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().setAcceptFileSchemeCookies(true); <- THIS
CookieManager.getInstance().setAcceptThirdPartyCookies(webview, true); <- THIS
webview.setWebViewClient(new mWebViewClient());
webview.getSettings().setAppCacheEnabled(true);
...