개발/안드로이드

Android WebView를 포함하는 앱에서 브라우저를 여는 대신 리다이렉션 처리하기

맨날치킨 2022. 12. 13. 09:05
반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Android WebView, how to handle redirects in app instead of opening a browser

Android WebView, 브라우저를 여는 대신 앱에서 리다이렉션을 처리하는 방법

 문제 내용 

So right now in my app the URL I'm accessing has a redirect, and when this happens the WebView will open a new browser, instead of staying in my app. Is there a way I can change the settings so the View will redirect to the URL like normal, but stay in my app instead of opening a new browser?

그래서 지금 제 앱에서 제가 액세스하고 있는 URL은 리다이렉션되어 있고, 이런 일이 발생하면 웹 뷰는 내 앱에 머무르는 대신 새 브라우저를 열 것입니다. View가 평소처럼 URL로 리다이렉션되지만 새 브라우저를 여는 대신 내 앱에 머물도록 설정을 변경할 수 있는 방법이 있을까요?

 

 

 

 높은 점수를 받은 Solution 

Create a WebViewClient, and override the shouldOverrideUrlLoading method.

WebViewClient를 만들고 wouldOverrideUrlLoading 메서드를 재정의합니다.
webview.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        // do your handling codes here, which url is the requested url
        // probably you need to open that url rather than redirect:
        view.loadUrl(url);
        return false; // then it is not handled by default action
   }
});

 

 

 가장 최근 달린 Solution 

Please use the below kotlin code

아래 코틀린 코드를 사용하세요.
webview.setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {   
                view.loadUrl(url)
                return false 
            }
        })

 

For more info click here

자세한 내용은 여기를 클릭하세요.

 

 

 

출처 : https://stackoverflow.com/questions/4066438/android-webview-how-to-handle-redirects-in-app-instead-of-opening-a-browser

반응형