-->

안드로이드 로컬 웹뷰(file:///android_asset/index.html)에서 내부 파일로 페이지 Ajax 이동을 하려고 할 때 (subPage.html) cross origin 에러가 발생한다.

<div data-role="page" id="mainPage">
    <div data-role="header">
        <h1>mainPage Header</h1>
    </div>
    <div data-role="content">
        <h1>mainPage Content</h1>
        <p>
            <a href="subPage.html">subPage로 이동</a>
        </p>
    </div>
    <div data-role="footer">
        <h1>Footer</h1>
    </div>
</div>

 

다음과 같은 웹뷰 세팅으로 해결할 수 있다.

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        var webView: WebView = findViewById(R.id.web_view)
        
        webView.apply {
      		// Off by default, deprecated for SDK versions >= 30.
            settings.allowFileAccessFromFileURLs = true
            settings.allowUniversalAccessFromFileURLs = true
        }
    }

 

+ api 30부터 deprecated 되었기 때문에 다음과 같이 WebViewAssetLoader를 불러와야한다.

        val assetLoader = WebViewAssetLoader.Builder()
            .addPathHandler("/assets/", AssetsPathHandler(this))
            .addPathHandler("/res/", ResourcesPathHandler(this))
            .build()

        webView.webViewClient = object : WebViewClient() {
            override fun shouldInterceptRequest(
                view: WebView?,
                request: WebResourceRequest
            ): WebResourceResponse? {
                return assetLoader.shouldInterceptRequest(request.url)
            }
        }

<참고>

developer.android.com/reference/androidx/webkit/WebViewAssetLoader

 

WebViewAssetLoader  |  Android 개발자  |  Android Developers

WebViewAssetLoader public final class WebViewAssetLoader extends Object java.lang.Object    ↳ androidx.webkit.WebViewAssetLoader Helper class to load local files including application's static assets and resources using http(s):// URLs inside a WebVie

developer.android.com

pilot376.tistory.com/68stackoverflow.com/questions/63054818/websetting-allowfileaccessfromfileurls-is-deprecated-in-api-lev el-30

 

WebSetting allowFileAccessFromFileURLs is Deprecated in API Level 30

I wanted to use webSetting allowFileAccessFromFileURLs in WebView but the setter is deprecated. how should I set this parameter.

stackoverflow.com

 

+ Recent posts