Skip to content

Commit 3d85dfd

Browse files
authored
Update webview snippet to native implementation (#682)
1 parent 2489cb2 commit 3d85dfd

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.identity.credentialmanager
18+
19+
import android.os.Bundle
20+
import android.util.Log
21+
import android.webkit.WebView
22+
import androidx.activity.ComponentActivity
23+
import androidx.activity.compose.setContent
24+
import androidx.compose.ui.viewinterop.AndroidView
25+
import androidx.webkit.WebSettingsCompat
26+
import androidx.webkit.WebViewFeature
27+
28+
// [START android_identity_webview_main_activity]
29+
class WebViewActivity : ComponentActivity() {
30+
override fun onCreate(savedInstanceState: Bundle?) {
31+
super.onCreate(savedInstanceState)
32+
setContent {
33+
val url = "https://passkeys-codelab.glitch.me/"
34+
AndroidView(
35+
factory = { context ->
36+
WebView(context).apply {
37+
settings.javaScriptEnabled = true
38+
39+
webViewClient = WebViewClientImpl()
40+
}
41+
},
42+
update = { webView ->
43+
run {
44+
webView.loadUrl(url)
45+
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_AUTHENTICATION)) {
46+
WebSettingsCompat.setWebAuthenticationSupport(
47+
webView.settings,
48+
WebSettingsCompat.WEB_AUTHENTICATION_SUPPORT_FOR_APP,
49+
)
50+
// Check if getWebauthenticationSupport may have been disabled by the WebView.
51+
Log.e(
52+
"WebViewPasskeyDemo",
53+
"getWebAuthenticationSupport result: " + WebSettingsCompat.getWebAuthenticationSupport(
54+
webView.settings
55+
),
56+
)
57+
} else {
58+
Log.e("WebViewPasskeyDemo", "WebView does not support passkeys.")
59+
}
60+
}
61+
},
62+
)
63+
}
64+
}
65+
}
66+
// [END android_identity_webview_main_activity]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.identity.credentialmanager
18+
19+
import android.webkit.WebView
20+
import android.webkit.WebViewClient
21+
22+
class WebViewClientImpl : WebViewClient() {
23+
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
24+
return false
25+
}
26+
}

0 commit comments

Comments
 (0)