개발/Android

webview - window.open() 처리

Ricetherapy 2021. 6. 2. 18:20

WebChromeClient를 상속받은 클래스를 생성하여 아래 펑션을 작성한다.

    override fun onCreateWindow(view: WebView?, isDialog: Boolean, isUserGesture: Boolean, resultMsg: Message?): Boolean {
        val newWebView = WebView(mContext)
        newWebView.settings.javaScriptEnabled = true

        var dialog = Dialog(view?.context)
        dialog.setContentView(newWebView)

        val params: ViewGroup.LayoutParams = dialog.window.attributes
        params.width = ViewGroup.LayoutParams.MATCH_PARENT
        params.height = ViewGroup.LayoutParams.MATCH_PARENT
        dialog.window.attributes = params as WindowManager.LayoutParams
        dialog.show()

        newWebView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                if (url.startsWith("intent:")) {
                    try {
                        var intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                        mContext.startActivity(intent)
                    } catch (e: Exception) {}
                } else {
                    view.loadUrl(url)
                }
                return true
            }
        }
        newWebView.webChromeClient = object : WebChromeClient() {
            override fun onCloseWindow(window: WebView?) {
                Qlog.i(">>> closeWindow 호출")
                dialog.dismiss()
            }
        }

        val transport = resultMsg?.obj as WebView.WebViewTransport
        transport.webView = newWebView
        resultMsg.sendToTarget()
        return true
    }

 

이후 웹뷰에서 자바스크립트를 사용할 수 있게 설정해준다.

 

        idWebView.settings.javaScriptEnabled = true
        idWebView.settings.javaScriptCanOpenWindowsAutomatically = true
        idWebView.settings.setSupportMultipleWindows(true)