Skip to content

Commit 90f6ac6

Browse files
committed
feat: implement cookie manager for persistent session
1 parent 1d1876d commit 90f6ac6

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

app/src/main/java/com/mrepol742/webappp/WebViewScreen.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.content.Intent
66
import android.os.Build
77
import android.util.Log
88
import android.view.ViewGroup
9+
import android.webkit.CookieManager
910
import android.webkit.WebSettings
1011
import android.webkit.WebView
1112
import androidx.activity.compose.BackHandler
@@ -81,6 +82,10 @@ fun WebViewScreen(
8182
scrollBarStyle = WebView.SCROLLBARS_INSIDE_OVERLAY
8283
overScrollMode = WebView.OVER_SCROLL_NEVER
8384

85+
val cookieManager = CookieManager.getInstance()
86+
cookieManager.setAcceptCookie(true);
87+
cookieManager.setAcceptThirdPartyCookies( this, true)
88+
8489
val currentUA = settings.userAgentString
8590
val safeAppName = appName.replace("\\s+".toRegex(), "")
8691
settings.userAgentString = "$currentUA $safeAppName/$appVersion"
@@ -97,7 +102,8 @@ fun WebViewScreen(
97102
context as Activity,
98103
fileChooserLauncher,
99104
locationPermissionLauncher,
100-
permissionsLauncher
105+
permissionsLauncher,
106+
cookieManager
101107
)
102108
webChromeClient = chromeClient
103109
secureWebChromeClientState.value = chromeClient

app/src/main/java/com/mrepol742/webappp/client/SecureChromeClient.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.content.pm.PackageManager
88
import android.net.Uri
99
import android.view.View
1010
import android.view.ViewGroup
11+
import android.webkit.CookieManager
1112
import android.webkit.GeolocationPermissions
1213
import android.webkit.JsPromptResult
1314
import android.webkit.JsResult
@@ -23,7 +24,8 @@ class SecureChromeClient(
2324
private val activity: Activity,
2425
private val fileChooserLauncher: ActivityResultLauncher<Intent>,
2526
private val locationPermissionLauncher: ActivityResultLauncher<String>,
26-
private val permissionsLauncher: ActivityResultLauncher<Array<String>>
27+
private val permissionsLauncher: ActivityResultLauncher<Array<String>>,
28+
private val cookieManager: CookieManager
2729
) : WebChromeClient() {
2830

2931
private var filePathCallback: ValueCallback<Array<Uri>>? = null
@@ -32,6 +34,10 @@ class SecureChromeClient(
3234
private var fullScreenContainer: FrameLayout? = null
3335
private var pendingRequest: PermissionRequest? = null
3436

37+
override fun onProgressChanged(view: WebView?, newProgress: Int) {
38+
cookieManager.flush();
39+
}
40+
3541
override fun onShowFileChooser(
3642
webView: WebView?,
3743
filePathCallback: ValueCallback<Array<Uri>>,

0 commit comments

Comments
 (0)