如何在WebView中为本地js文件启用Cookie? [英] How to enable cookie in webview for local js file?

查看:76
本文介绍了如何在WebView中为本地js文件启用Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WebView中接受cookie,但是会收到错误消息

I want to accept cookies in WebView, but get error

未捕获的错误:SECURITY_ERR:DOM异常18

Uncaught Error: SECURITY_ERR: DOM Exception 18

该问题仅发生在本地js文件中.如Android文档所说,我必须使用 setAcceptFileSchemeCookies

The problem occures just in local js file. As Android docs say, I must use setAcceptFileSchemeCookies

但这不能解决我的问题.我不知道我在做什么错.

But it doesn't solve my problem. I don't know what I'm doing wrong.

全部代码列表:

protected override void OnCreate(Bundle bundle) {
        try {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            _webView = FindViewById<WebView>(Resource.Id.webview);
            string html = string.Empty;
            Android.Webkit.CookieManager.SetAcceptFileSchemeCookies(true);               
            using (var stream = _webView.Context.Assets.Open("res/mobileLogin.html")) {
                using (TextReader reader = new StreamReader(stream)) {
                    html = reader.ReadToEnd();
                }
            }
            _webView.Settings.JavaScriptEnabled = true;
            _webView.Settings.AllowFileAccess = true;
            _webView.Settings.AllowUniversalAccessFromFileURLs = true;
            _webView.SetWebViewClient(new HybridWebViewClient());
            _webView.SetWebChromeClient(new WebChromeClient());
            _webView.LoadDataWithBaseURL("file:///android_asset/res/", html, "text/html", "UTF-8", null);
            Android.Webkit.CookieManager.Instance.SetAcceptCookie(true);

        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }

推荐答案

据我所知,在Android上将Cookie主机设置为 localhost 会触发安全异常.如果您对远程主机尝试相同的操作,则可能会起作用.那当然不能解决您的问题.像这样的东西: https://stackoverflow.com/a/24897724/368379 可以提供一个自己处理东西的起点,但与Cookies并不完全相同.

As far as I can read, setting the cookie host to localhost on Android triggers a security exception. If you try the same with a remote host it will probably work. That, of course does not solve your problem. Something like this: https://stackoverflow.com/a/24897724/368379 could provide a starting point to handle stuff yourself, but not exactly the same as Cookies.

如果您使用JavascriptInterface路由,则可以像这样在Xamarin.Android中实现该路由:

If you go the JavascriptInterface route, you can implement that in Xamarin.Android like so:

public class MyJavascriptInterface : Java.Lang.Object
{
    [Export("DoStuff")]
    [JavascriptInterface]
    public void DoStuff(string stuff)
    {
        // do something here with the string
    }
}

var webView = new WebView(this);
webView.Settings.JavaScriptEnabled = true;
var interface = new MyJavascriptInterface();
webView.AddJavascriptInterface(interface, "MyJavascriptInterface");

然后您可以像这样用Java语言调用它:

Then you can call it in Javascript like so:

MyJavascriptInterface.DoStuff("My Awesome String");

这篇关于如何在WebView中为本地js文件启用Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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