모바일 어플리케이션에서 웹뷰 이용하기(Android)

Android 앱에서 웹뷰를 사용하는 경우 다음 내용을 참고해주세요.

WebViewClient

fun shouldOverrideUrlLoading

shouldOverrideUrlLoading function 내에 webView.loadUrl(url)을 넣으면 안됩니다.

만약, 별도 스킴이 있을 경우 다음 코드를 참고하여 해당 function에 정의해주세요.

webView.webViewClient = object : WebViewClient() {
    override fun shouldOverrideUrlLoading(
        view: WebView?,
        request: WebResourceRequest?
    ): Boolean {
        // if you have custom scheme
        val url = request?.url ?: return super.shouldOverrideUrlLoading(view, request)
        if (url.toString().startsWith("market")) {
            val intent = Intent(Intent.ACTION_VIEW).apply {
                data = url
            }
            startActivity(intent)
            return true
        }
        return super.shouldOverrideUrlLoading(view, request)
    }
}



WebChromeClient

fun onCreateWindow

  1. 상품 클릭 시 창 전환을 원할 경우 다음 코드를 참고해주세요.
webView.webChromeClient = object : WebChromeClient() {
    override fun onCreateWindow(
        view: WebView?,
        isDialog: Boolean,
        isUserGesture: Boolean,
        resultMsg: Message?
    ): Boolean {
        view ?: return true
        resultMsg ?: return true
        val subWebView = WebView(view.context)
        subWebView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                url ?: return false
                if (url.startsWith("http")) {
                    // if you want to open current webView
                    webView.loadUrl(url)
                }
                return true
            }
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                val url = request?.url?.toString() ?: return false
                if (url.startsWith("http")) {
                    // if you want to open current webView
                    webView.loadUrl(url)
                }
                return true
            }
        }
        val viewTransport = (resultMsg.obj as? WebView.WebViewTransport) ?: return true
        viewTransport.webView = subWebView
        resultMsg.sendToTarget()
        return true
    }
}

  1. 상품 클릭 시 새 창으로 열기를 원할 경우 다음 코드를 참고해주세요.
webView.webChromeClient = object : WebChromeClient() {
    override fun onCreateWindow(
        view: WebView?,
        isDialog: Boolean,
        isUserGesture: Boolean,
        resultMsg: Message?
    ): Boolean {
        view ?: return true
        resultMsg ?: return true
        val subWebView = WebView(view.context)
        subWebView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                url ?: return false
                if (url.startsWith("http")) {
                    // if you want to open new page
                    val intent = Intent(Intent.ACTION_VIEW).apply {
                        data = Uri.parse(url)
                    }
                    startActivity(intent)
                }
                return true
            }
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                val url = request?.url?.toString() ?: return false
                if (url.startsWith("http")) {
                    // if you want to open new page
                    val intent = Intent(Intent.ACTION_VIEW).apply {
                        data = Uri.parse(url)
                    }
                    startActivity(intent)
                }
                return true
            }
        }
        val viewTransport = (resultMsg.obj as? WebView.WebViewTransport) ?: return true
        viewTransport.webView = subWebView
        resultMsg.sendToTarget()
        return true
    }
}