Android WebView中Web NFC API权限请求被拒绝(NotAllowedError)的解决方案咨询
Hey there! Let's work through this Web NFC permission issue in your Android WebView. That NotAllowedError pops up because WebView needs a bit more setup beyond just enabling JavaScript to grant access to NFC features. Here's what you need to fix step by step:
1. Add NFC Permissions to Your Manifest
First, you need to declare that your app uses NFC in the AndroidManifest.xml file. Add these lines right before the <application> tag:
<uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" />
- The
<uses-permission>line lets your app request access to NFC hardware. - The
<uses-feature>line tells Google Play to only show your app to devices that support NFC (you can setrequired="false"if you want your app to work on non-NFC devices too).
2. Handle Permission Requests in WebView
WebView won't automatically prompt for or grant NFC permissions like a regular browser does. You need to set up a WebChromeClient and override its onPermissionRequest method to explicitly grant NFC access when requested by the web content.
Update your MainActivity.kt code with these changes:
- Add imports for
android.webkit.WebChromeClientandandroid.webkit.PermissionRequest. - Replace your existing WebView setup with code that includes the
WebChromeClientoverride.
Here's how the updated onCreate method will look:
@SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) webView = findViewById(R.id.webView) urlInput = findViewById(R.id.urlInput) goButton = findViewById(R.id.goButton) webView.webViewClient = WebViewClient() webView.settings.javaScriptEnabled = true // Add this WebChromeClient to handle NFC permission requests webView.webChromeClient = object : WebChromeClient() { override fun onPermissionRequest(request: PermissionRequest?) { request?.let { permissionRequest -> // Check if the request is for NFC access if (permissionRequest.resources.contains(PermissionRequest.RESOURCE_NFC)) { // Grant the NFC permission permissionRequest.grant(permissionRequest.resources) } else { // Deny other unneeded permissions (adjust as per your app's needs) permissionRequest.deny() } } } } goButton.setOnClickListener { var url = urlInput.text.toString().trim() if (!url.startsWith("http://") && !url.startsWith("https://")) { url = "https://$url" } webView.loadUrl(url) } }
3. Ensure Minimum API Level is 29 or Higher
Web NFC support in Android WebView was introduced in Android 10 (API level 29). Open your module-level build.gradle file and set the minimum SDK version to at least 29:
android { defaultConfig { minSdkVersion 29 targetSdkVersion 34 // Use the latest stable SDK version } }
4. Optional: Add Device NFC Checks (For Better User Experience)
To avoid confusion, you can add code to check if the device supports NFC and if it's enabled before loading the web content. Add this inside your goButton click listener:
val nfcAdapter = NfcAdapter.getDefaultAdapter(this) if (nfcAdapter == null) { Toast.makeText(this, "This device doesn't support NFC", Toast.LENGTH_SHORT).show() return@setOnClickListener } if (!nfcAdapter.isEnabled) { // Prompt user to turn on NFC in settings val intent = Intent(Settings.ACTION_NFC_SETTINGS) startActivity(intent) return@setOnClickListener }
For this, you'll need to add these imports at the top of MainActivity.kt:
import android.nfc.NfcAdapter import android.widget.Toast import android.provider.Settings
Final Notes
- Always test on a physical Android device (emulators don't support NFC functionality).
- Make sure the web content you're loading uses HTTPS (your code already handles this automatically, which is perfect because Web NFC only works with secure origins).
Give these changes a try, and your WebView should now grant the necessary NFC permissions to the web content! Let me know if you run into any other issues along the way.




