如何在Android中实现Web视图的多登录? [英] How to achieve multi-login for webviews in Android?

查看:71
本文介绍了如何在Android中实现Web视图的多登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个允许用户使用不同的网络视图登录同一网站的多个帐户的应用.

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

例如,我有2个WebView.
每个WebView将加载相同的网站,例如gmail.com.
用户可以在单独的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.

但是我面临的问题是2个WebView 始终登录到同一帐户.

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

我已经用Google搜索了很多,这里有一些相关的标题,
Android Webview中的Facebook MultiLogin
使用WebView进行多页登录到网站和提取数据
在单独的WebView上多次登录?(Android)
但仍然找不到可接受的答案.

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.

在Android中可以使用WebView吗?
如何实现我想要的?

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

推荐答案

棘手的部分是android.webkit.CookieManager,由WebView用来保存cookie,它被设计为单例.这意味着每个Java/Dalvik进程只有一个CookieManager实例,并且同一进程内的多个WebView实例共享同一组Cookie.

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.

就像@ToYonos提议的那样,您可以尝试重写某些挂钩来解决此问题,但是我认为它不会100%起作用...还要考虑android.webkit.WebStorage:这是另一个单例!

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!

也就是说,这可能更可靠地起作用: :在清单中复制您的顶级WebView活动并分配它们在不同的进程中运行:

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

这样您将具有隔离的进程和不同的CookieManager/WebStorage实例.

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

但请注意:不同的WebStorage实例仍会写入应用程序数据文件夹中的相同路径!可以通过调用 webView.getSettings来解决().setDatabasePath()来为不同的进程使用不同的db路径,但是此API在API级别19(KitKat)中已弃用.只要您访问的网页不使用HTML5本地存储,就可以了...

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

这篇关于如何在Android中实现Web视图的多登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆