AndroidアプリのWebViewでCookieが取得できない問題の解決方法
AndroidアプリのWebViewでcookieを取得できない問題が発生した時の対応ログが残っていたのでここに上げておきます。
結論から言うと、Android 5.0からサードパーティのCookieが弾かれていたのが原因でした。
混合コンテンツとサードパーティの Cookie がデフォルトでブロックされます。混合コンテンツとサードパーティの Cookie を許可するには、それぞれ setMixedContentMode() メソッドと setAcceptThirdPartyCookies() メソッドを使用します。
具体的にはsetAcceptThirdPartyCookies()
メソッドを使ってサードパーティのCookieを許可していないのが原因でした。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.setAcceptThirdPartyCookies(binding.webView, true);
}
Lollipop(Android 5.0)より下のバージョンをサポートしている場合は、上記のように分岐をしてあげないとクラッシュします。
デバッグ方法
単純にログを吐くだけです。
String[] cookies = cookieManager.getCookie(url).split(";");
for(String cookie : cookies) {
Log.d("onPageStarted", cookie);
}
Chromeを使ってWebViewをみる
以下の記事の方法でNetworkやConsoleを見れたことが解決に繋がりました。
WebView のリモート デバッグ
https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews?hl=ja
Author And Source
この問題について(AndroidアプリのWebViewでCookieが取得できない問題の解決方法), 我々は、より多くの情報をここで見つけました https://qiita.com/yamshta/items/21796a3cfd1cc3b1cf11著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .