I have two Applications with two web-views (a web-view in each). to simply explain my requirement with an example consider a user adds few items in his shopping trolley in first web-view in first app and then he press checkout and this will launched second web-view (which is in second app), result : he needs to see same shopping cart (in second web-view too).
I know sharing cookies between two webviews in a single app is not a problem and it is done in background by Android (CookieManager) however my case is different, I need to achieve the same in two different App.
Perhaps what I am looking for is how to share cookies/ session cookies between two webviews in different apps?
Note: I am restricted to targeted API 18
What I tried so far (apart of lots of useless reading) is :
- getting cookie of the URL from the first webview and send it through intent to second APP's activity
mWebView.setWebChromeClient(new WebChromeClient() {
});
mWebView.loadUrl(getPreferedURL().toString());
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!url.contains("openCheckoutPage")) {
return false;
} else {
mCookie = CookieManager.getInstance().getCookieurl);
Intent fullScreenIntent = new Intent("com.bastami1982.webview.app.WebviewPanel_ACTION");
fullScreenIntent.putExtra("mUrl", mUrl);
fullScreenIntent.putExtra("mCookie", mCookie);
startActivity(fullScreenIntent);
return true;
}
}
});
- set the cookie in second app webview
private void loadFullWebview() {
mLink = getIntent().getStringExtra("mUrl");
mLinkCookie = getIntent().getStringExtra("mCookie");
mFullScreenWebView = (WebView) findViewById(R.id.webView);
mFullScreenWebView.setWebChromeClient(new WebChromeClient() {
});
WebSettings settings = mFullScreenWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
if (mLinkCookie!=null) {
mCoockieManager.setAcceptCookie(true);
mCoockieManager.setCookie(mLink, mLinkCookie);
mCoockieManager.acceptCookie();
CookieSyncManager.getInstance().sync();
}
mFullScreenWebView.loadUrl(mLink);
mFullScreenWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
}
I can confirm that I will get the cookie in second app but it seems webview is not using the cookie because the shopping cart is still empty.
I hope I explained it good enough to have some feedback , having said that please ask me for more info if it is needed
I checked the logcat in second App and I think this means that items in trolley can not be find (getItem) (I used Argos.co.uk) to test their shopping Trolley in another word I think cookie is not attached to the url correctly at loading ?! just guessing...
12-18 16:18:11.754 14675-14675 I/Web Console: Error while sending custom tracking data at https://d1af033869koo7.cloudfront.net/psp/argos-v1-001/default/20151216103020/pxfwk.gz.js:7
12-18 16:18:11.904 14675-14675 E/Web Console: Uncaught TypeError: Cannot call method 'getItem' of null at https://argospsp.px.247inc.net/pspxd.html#parentdomain=https://www.argos.co.uk&clientkey=argos-v1-001&version=20151216103020&pspv=default&n=1&caller=refloaded&l=1000&s=cookie:5