最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cookies are not working in an iframe in Android Webview - Stack Overflow

programmeradmin2浏览0评论

One user of my Webview based browser app reported an issue with embedding Google Calendar in an iframe. It looks like the iframe loaded by Android Webview (latest version from Google Play, tested with Android 6) can't read or set any cookies.

The issue is not present in Google Chrome on the same device. It seems to be related to Android Webview only.

I can reproduce the issue with following site.

<!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    </head>
    <body>
    <a href=".asp">Cookies Management</a>
    <br><br>
    <iframe style="margin-top: 30px; border-width: 1px;" src=".asp" width="400" height="672" scrolling="yes"></iframe>
    </body>
    </html>

One user of my Webview based browser app reported an issue with embedding Google Calendar in an iframe. It looks like the iframe loaded by Android Webview (latest version from Google Play, tested with Android 6) can't read or set any cookies.

The issue is not present in Google Chrome on the same device. It seems to be related to Android Webview only.

I can reproduce the issue with following site.

<!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    </head>
    <body>
    <a href="http://www.w3schools./js/js_cookies.asp">Cookies Management</a>
    <br><br>
    <iframe style="margin-top: 30px; border-width: 1px;" src="http://www.w3schools./js/js_cookies.asp" width="400" height="672" scrolling="yes"></iframe>
    </body>
    </html>

If you open the cookie site via link in the main frame you can set/read cookies by the buttons "Create Cookie..." or "Display All Cookies". In the iframe however displaying all cookies always returns an empty result. Even if you create a cookie in the iframe displaying all cookies then gets an empty result.

Are there any Webview settings affecting this behaviour?

Share Improve this question edited Jul 30, 2016 at 10:11 Krishna Kachhela 7931 gold badge7 silver badges24 bronze badges asked Jul 30, 2016 at 9:15 Alexey OzerovAlexey Ozerov 1,54815 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

I don't know why but the behaviour can be improved by enabling third party cookies like this:

    if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);

After doing so the cookies are working in an iFrame as expected.

发布评论

评论列表(0)

  1. 暂无评论