웹뷰 이용하기
- 인쇄
웹뷰 이용하기
- 인쇄
기사 요약
이 요약이 도움이 되었나요?
의견을 보내 주셔서 감사합니다.
Android 앱에서 웹뷰를 사용하는 경우 다음 내용을 참고해주세요.
WebViewClient
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
onCreateWindow
상품 클릭 시 창 전환을 원할 경우 다음 코드를 참고해주세요.
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
}
}
상품 클릭 시 새 창으로 열기를 원할 경우 다음 코드를 참고해주세요.
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
}
}