How to achieve multi-login for webviews in Android?

倾然丶 夕夏残阳落幕 提交于 2021-02-06 12:44:31

问题


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?


回答1:


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...




回答2:


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


来源:https://stackoverflow.com/questions/26323050/how-to-achieve-multi-login-for-webviews-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!