13
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WebViewのshouldOverrideUrlLoadingでSPAのURLローディングを検知できない場合の対処法

Last updated at Posted at 2018-06-29

はじめに

AndroidのWebViewでURLローディングを検知する際、 shouldOverrideUrlLoading を使うのがよくある方法だと思いますが、POSTリクエストの場合、この方法ではイベントの検知が出来ません。

やり方

WebViewClientの doUpdateVisitedHistory を使います。
doUpdateVisitedHistory は訪問済みリンクのデータベースを更新するメソッドです。

mWebView.setWebViewClient( new WebViewClient(){
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                Log.d(TAG, "shouldOverrideUrlLoading LOLLIPOP url = " + request.getUrl().toString());
                return false;
            }

            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.d(TAG, "shouldOverrideUrlLoading url = " + url);
                return false;
            }

            @Override
            public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
                Log.d(TAG, "doUpdateVisitedHistory url = " + url);
            }
        });

では、WebViewからSPAにアクセスしてみます。

06-29 10:10:50.756 13453-13453/jp.sample D/WebViewFragment: doUpdateVisitedHistory url = https://xxxx/profile/home
06-29 10:34:41.636 13453-13453/jp.sample D/WebViewFragment: doUpdateVisitedHistory url = https://xxxx/connect
06-29 10:34:41.658 13453-13453/jp.sample D/WebViewFragment: doUpdateVisitedHistory url = https://xxxx/logout
06-29 10:34:41.671 13453-13453/jp.sample D/WebViewFragment: doUpdateVisitedHistory url = https://xxxx/connect
06-29 10:34:44.131 13453-13453/jp.sample D/WebViewFragment: doUpdateVisitedHistory url = https://xxxx/signin
06-29 10:34:54.526 13453-13453/jp.sample D/WebViewFragment: doUpdateVisitedHistory url = https://xxxx/auth/callback?token=xxxx

無事、検知できました!

13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?