Skip to content

Commit 0a91c50

Browse files
committed
add webviewassetloader & local file
1 parent 0d0a6d3 commit 0a91c50

5 files changed

Lines changed: 46 additions & 5 deletions

File tree

.idea/deploymentTargetSelector.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dependencies {
4848
implementation(libs.androidx.lifecycle.viewmodel.ktx)
4949
implementation(libs.androidx.navigation.fragment.ktx)
5050
implementation(libs.androidx.navigation.ui.ktx)
51+
implementation(libs.androidx.webkit)
5152
testImplementation(libs.junit)
5253
androidTestImplementation(libs.androidx.junit)
5354
androidTestImplementation(libs.androidx.espresso.core)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>CanIWebView</title>
5+
</head>
6+
<body>
7+
<h1>CanIWebView Test App</h1>
8+
<p>This is a file served in the app by
9+
<a href="https://developer.android.com/develop/ui/views/layout/webapps/load-local-content">
10+
WebViewAssetLoader
11+
</a>
12+
</p>
13+
14+
15+
<b>
16+
<a href="https://caniwebview.com/app">Check out caniwebview.com</a>
17+
</b>
18+
</body>

app/src/main/java/com/caniwebview/android/ui/webview/WebViewFragment.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import android.os.Bundle
77
import android.view.LayoutInflater
88
import android.view.View
99
import android.view.ViewGroup
10+
import android.webkit.WebResourceRequest
11+
import android.webkit.WebResourceResponse
1012
import android.webkit.WebSettings
1113
import android.webkit.WebView
12-
import android.webkit.WebViewClient
1314
import android.widget.Button
1415
import android.widget.EditText
1516
import androidx.fragment.app.Fragment
17+
import androidx.webkit.WebViewAssetLoader
18+
import androidx.webkit.WebViewClientCompat
1619
import com.caniwebview.android.databinding.FragmentWebviewBinding
1720

1821
class WebViewFragment : Fragment() {
@@ -38,10 +41,27 @@ class WebViewFragment : Fragment() {
3841
val urlEditText: EditText = binding.urlEditText
3942
val loadButton: Button = binding.loadButton
4043

41-
webView.webViewClient = WebViewClient()
44+
// Create the asset loader
45+
// Create the asset loader with a custom domain
46+
val assetLoader = WebViewAssetLoader.Builder()
47+
.setDomain("caniwebview.local") // Set your custom domain here
48+
.setHttpAllowed(false) // Enforce HTTPS (recommended)
49+
.addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(requireContext()))
50+
.build()
51+
52+
53+
// Set the WebViewClient
54+
webView.webViewClient = object : WebViewClientCompat() {
55+
override fun shouldInterceptRequest(
56+
view: WebView,
57+
request: WebResourceRequest
58+
): WebResourceResponse? {
59+
return assetLoader.shouldInterceptRequest(request.url)
60+
}
61+
}
4262

4363
// Load url in field if saved
44-
val savedUrl = sharedPreferences.getString("url", "https://caniwebview.com/app")
64+
val savedUrl = sharedPreferences.getString("url", "https://caniwebview.local/assets/html/index.html")
4565
if (savedUrl != null) {
4666
urlEditText.setText(savedUrl)
4767
webView.loadUrl(savedUrl)

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ lifecycleLivedataKtx = "2.6.1"
1212
lifecycleViewmodelKtx = "2.6.1"
1313
navigationFragmentKtx = "2.6.0"
1414
navigationUiKtx = "2.6.0"
15+
webkit = "1.12.1"
1516

1617
[libraries]
1718
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -25,6 +26,7 @@ androidx-lifecycle-livedata-ktx = { group = "androidx.lifecycle", name = "lifecy
2526
androidx-lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" }
2627
androidx-navigation-fragment-ktx = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigationFragmentKtx" }
2728
androidx-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigationUiKtx" }
29+
androidx-webkit = { group = "androidx.webkit", name = "webkit", version.ref = "webkit" }
2830

2931
[plugins]
3032
android-application = { id = "com.android.application", version.ref = "agp" }

0 commit comments

Comments
 (0)