Sunday, March 15, 2026
HomeiOS Developmentios - Can't get microphone to work correctly in WebView

ios – Can’t get microphone to work correctly in WebView


I am having hassle getting the microphone to work inside a WebView.

There is a webpage that I do know I can use to check my microphone. It really works completely in Safari. Once I speak, I can see the bursts on the take a look at space that correspond to what I am saying.

I wrote an app to show the location in a WebView. I run the app, click on Permit when it asks if I wish to permit the location to make use of my microphone. Once I speak, the bursts on the take a look at space do NOT correspond to what I am saying. In truth, the bursts in a form of heartbeat sample. It is prefer it does not hear my voice in any respect. It looks like a take a look at sample.

I really feel like I am doing one thing clearly improper. Any assist can be appreciated.

I am working the next:

  • Sonoma 14.6.1
  • Xcode 15.4
  • Simulator 15.4 (iPhone 15 professional)

This is the code:

ContentView.swift

import UIKit
import WebKit
import AVFoundation

class WebViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
    var webView: WKWebView!

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        // Setup WKWebView
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(body: .zero, configuration: webConfiguration)
        webView.navigationDelegate = self
        webView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(webView)
        
        // Add script message handler
        let contentController = WKUserContentController()
        contentController.add(self, identify: "logHandler")
        webConfiguration.userContentController = contentController

        // Add Auto Structure constraints
        NSLayoutConstraint.activate([
            webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            webView.topAnchor.constraint(equalTo: view.topAnchor),
            webView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])

        openMicrophoneTestPage()
    }
    

    personal func openMicrophoneTestPage() {
        let testURL = URL(string: "https://mictests.com/")!
        let request = URLRequest(url: testURL)
        webView.load(request)
    }
    
    // WKScriptMessageHandler methodology
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.identify == "logHandler" {
            if let messageBody = message.physique as? String {
                print("JavaScript log: (messageBody)")
            }
        }
    }
}

WebViewControllerWrapper.swift

import SwiftUI
import UIKit

struct WebViewControllerWrapper: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> WebViewController {
        return WebViewController()
    }

    func updateUIViewController(_ uiViewController: WebViewController, context: Context) {
        // No replace logic wanted
    }
}

MicrophoneApp.swift

import SwiftUI

@primary
struct MicrophoneApp: App {
    var physique: some Scene {
        WindowGroup {
            WebViewControllerWrapper()
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments