 
  		        lochanchugh
Microphone input in website in webview for swiftui
I have a website of a chatbot that also accepts microphone inputs and I embedded that into my swift and the everything other than the mic is working. I have included permission in the info file and here’s some of the code :
import SwiftUI
import WebKit
import AVFoundation
struct WebView: UIViewRepresentable {
    let url: URL
    class Coordinator: NSObject, WKUIDelegate, WKNavigationDelegate {
        var parent: WebView
        init(parent: WebView) {
            self.parent = parent
        }
        func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
            decisionHandler(.allow)
        }
        func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
            let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
                completionHandler()
            }))
            parent.presentAlert(alert: alert)
        }
        func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
            let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
                completionHandler(true)
            }))
            alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
                completionHandler(false)
            }))
            parent.presentAlert(alert: alert)
        }
        func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
            let alert = UIAlertController(title: prompt, message: nil, preferredStyle: .alert)
            alert.addTextField { textField in
                textField.text = defaultText
            }
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
                completionHandler(alert.textFields?.first?.text)
            }))
            alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
                completionHandler(nil)
            }))
            parent.presentAlert(alert: alert)
        }
        func webView(_ webView: WKWebView, requestMediaCapturePermissionFor origin: WKSecurityOrigin, initiatedByFrame frame: WKFrameInfo, type: WKMediaCaptureType, decisionHandler: @escaping (WKPermissionDecision) -> Void) {
            decisionHandler(.grant)
        }
        func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
            print("Navigation error: \(error.localizedDescription)")
        }
    }
    func makeCoordinator() -> Coordinator {
        Coordinator(parent: self)
    }
    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true 
        webConfiguration.mediaTypesRequiringUserActionForPlayback = []
        let webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = context.coordinator
        webView.navigationDelegate = context.coordinator
        requestMicrophoneAccess()
        return webView
    }
    func updateUIView(_ webView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        webView.load(request)
    }
    func requestMicrophoneAccess() {
        AVAudioSession.sharedInstance().requestRecordPermission { granted in
            DispatchQueue.main.async {
                if granted {
                    print("Microphone access granted")
                } else {
                    print("Microphone access denied")
                }
            }
        }
    }
    func presentAlert(alert: UIAlertController) {
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
           let rootViewController = windowScene.windows.first?.rootViewController {
            rootViewController.present(alert, animated: true, completion: nil)
        }
    }
}
Popular Ios topics
                         
                      
                       
          
                If I install Catalina on unsupported models of MacBook Pro through a patcher, will there be any (certification or other) problem while co...
              
            
            
          
              New
 
          
                Hi! How effectively caching data of networking in iOS Swift? What libraries, techniques are you use? Can you share good materials(books, ...
              
            
            
          
              New
 
          
                Hi all, 
we want to develop an iPhone IOS app that will have 3 sections/uses and we would like your opinion if it can all be done and all...
              
            
            
          
              New
 
          
                does anyone have experience on swiftui? i was thinking about trying something new, unfortunately since my company is old i am not allowed...
              
            
            
          
              New
 
          
                HI, 
I am Mehmood khan Computer Science Student from Pakistan and I have a project in client want to made an automate application that wh...
              
            
            
          
              New
 
          
                Hi Everyone, I am Emmanuel Katto originally from Uganda. Now living in UK. I am new to this community. I want to know which blogs you peo...
              
            
            
          
              New
 
          
                I have a website of a chatbot that also accepts microphone inputs and I embedded that into my swift and the everything other than the mic...
              
            
            
          
              New
 
          
                Hey team I’m really wanting a retro i pod feel on my ipad revision 2 running 9.3.5 is there any one out there that can make this work or ...
              
            
            
          
              New
 
          
                We are trying to develop a coupon/offer code module where our app users can avail a free trail offer for 2 months period after applying t...
              
            
            
          
              New
Other popular topics
                         
                      
                       
          New
 
          
                I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
              
            
            
          
              New
 
          
                poll
poll
Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
              
            
            
              
          
              New
 
          
                I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. 
I think the Planck and the Preonic (o...
              
            
            
          
              New
 
          
                Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
              
            
            
              
          
              New
 
          
                zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. 
General-purpose programming language and toolchain for maintaini...
              
            
            
              
          
              New
 
          
                Explore the power of Ash Framework by modeling and building the domain for a real-world web application.
  
Rebecca Le @sevenseacat and ...
              
            
            
              
          
              New
 
          
                Hair Salon Games for Girls Fun 
Girls Hair Saloon game is mainly developed for kids. This game allows users to select virtual avatars to ...
              
            
            
          
              New
 
          
                Fight complexity and reclaim the original spirit of agility by learning to simplify how you develop software. The result: a more humane a...
              
            
            
              
          
              New
 
          
                Background
Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
              
            
            
          
              New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /rails
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /haskell
- /emacs
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /zig
- /deepseek
- /scala
- /sublime-text
- /lisp
- /textmate
- /react-native
- /nixos
- /debian
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /deno
- /ubuntu
- /nodejs
- /spring
- /manjaro
- /diversity
- /lua
- /julia
- /slackware
- /c

 
    




