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

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

问题描述

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

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.

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

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() 为不同的进程使用不同的数据库路径,但该 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中如何实现webview的多登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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