
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 I Os topics

Are there any improvements you think Apple could make to make things easier?
New

Core Data is intricate, powerful, and necessary. Harness its power with this guide to using Core Data in your iOS and OS X projects. In t...
New

New iPhones incoming… probably with 5G. Any other guesses? Anyone going to be watching it?
10 AM PDT
5 PM GMT
6 PM UK
New

A new Swift blog post/announcement has been posted!
Get the full details here: Swift.org - Introducing Swift Service Discovery
New

Hi! Do you use 3rd party UI inspector tools like Sherlock or Reveal? Which is better?
New

A new Swift blog post/announcement has been posted!
Get the full details here: Swift.org - The Future of Foundation
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

A new Swift blog post/announcement has been posted!
Get the full details here: Swift.org - Swift 5.8 Released!
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

A new Swift blog post/announcement has been posted!
New
Other popular topics

I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New

I’ve been really enjoying obsidian.md:
It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
New

I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New

On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New

Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New

A Hero’s Journey
with Chris Pine
@chrispine
Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New

Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New

Author Spotlight
Erin Dees
@undees
Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New

Author Spotlight:
Karl Stolley
@karlstolley
Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
Latest in iOS
Latest (all)
Categories:
Popular Portals
- /elixir
- /opensuse
- /rust
- /erlang
- /ruby
- /python
- /kotlin
- /clojure
- /react
- /go
- /js
- /react-native
- /swift
- /haskell
- /quarkus
- /debian
- /v
- /tailwind
- /vapor
- /gleam
- /nodejs
- /crystal
- /flutter
- /symfony
- /agda
- /julia
- /ubuntu
- /slackware
- /adonisjs
- /angular
- /php
- /ash
- /neovim
- /deepseek
- /c
- /grails
- /c-sharp
- /nest
- /diversity
- /revery
- /rubymotion
- /perl
- /giraffe
- /prolog
- /flask
- /amber
- /kemal
- /yew
- /hamler
- /groovy
- /lapis
- /fika
- /java
- /ocaml
- /c-plus-plus
- /security
- /typescript
- /zotonic
- /deno
- /wasm