Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve multi-login for webviews in Android?

I want to make an app that allows users to log-in multiple accounts of same site using different webview.

For example, I have 2 WebView.
Each WebView will load the same site such as gmail.com.
And user can log-in using separate account in separate WebView.

But the problem I am facing is that the 2 WebView always log-in to same account.

I've googled a lot, and here are some related titles,
Facebook MultiLogin in Android Webview
Using WebView for multi-page login to website and fetch data
Multiple Log-Ins on Separate WebViews? (Android)
but still no acceptable answer is found.

Would it be possible in Android using WebView?
How can I achieve what I want?

like image 270
Season Avatar asked Oct 12 '14 07:10

Season


People also ask

Can we save login data in Android WebView?

Saving passwords in WebView will not be supported in future versions. Retrieves HTTP authentication credentials for a given host and realm from the WebViewDatabase instance.

How do you communicate between WebView and native Android?

2 — Android: 2.1 To receive data from webview ,we can create an interface, which will enable webview to connect the native layer and pass data. From native layer, create a class and replicate the following. While configuring web view, we need to set JavaScript interface as above JSBridge class.

What is WebView implementation in Android?

If you want to deliver a web application (or just a web page) as a part of a client application, you can do it using WebView . The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout.

What is WebView link?

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application.


2 Answers

The tricky part is android.webkit.CookieManager, used by WebView to keep cookies, is designed to be a singleton. This means there'll be only one CookieManager instance per Java/Dalvik process, and your multi WebView instances inside the same process share a same set of cookies.

Like @ToYonos proposed, you may try overriding certain hooks to work around this, but I don't think it will 100% work...Also think about android.webkit.WebStorage: it's another singleton!

That said, this might work a bit more reliably: duplicate your top level WebView activity in manifest and assign them to run in different processes:

<activity
    android:name=".WebViewActivity" />
<activity
    android:name=".WebView1Activity"
    android:process=":web1" />
<activity
    android:name=".WebView2Activity"
    android:process=":web2" />
...

So that you'll have isolated processes and different CookieManager/WebStorage instances.

But be warned: different WebStorage instances still writes to same path(s) in your app data folder! This may be worked around by calling webView.getSettings().setDatabasePath() to use different db paths for different processes, but this API has been deprecated in API level 19 (KitKat). Well as long as the web page you're visiting doesn't use HTML5 local storage this should be fine...

like image 112
mindex Avatar answered Sep 22 '22 19:09

mindex


I think that you'll have to implement your own system. You could try something like that :

private static final String DOMAIN = "http://cookiedomain.com";

private final Map<WebView, String> cookiesMap = new HashMap<WebView, String>();

// [...]

WebView w = new WebView(this);
// Loading url and stuff
w.setWebViewClient(new WebViewClient()
{
    public void onLoadResource (WebView view, String url)
    {
        // If cookies have already been stored for this WebView
        if (cookiesMap.get(view) != null)
        {
            CookieManager.getInstance().removeAllCookie();
            CookieManager.getInstance().setCookie(DOMAIN, cookiesMap.get(view));
        }
    }

    public void onPageFinished(WebView view, String url)
    {
        // Check if the url matches the after-login page or whatever you want
        boolean condition = ...;  
        if(condition)
        {
            // Getting new cookies
            String cookies = CookieManager.getInstance().getCookie(DOMAIN);
            cookiesMap.put(view, cookies);
        }
    }
});

// Do the same for the 2nd WebView

This is a simple example, to be improved, but it could be a good start for a sustainable solution.

Limits :

  • It will only work if each WebView does not make request at the same time as others. Otherwise, it will surely tangle cookies.
  • This will work for one domain only
like image 35
ToYonos Avatar answered Sep 21 '22 19:09

ToYonos