Categories
nativePHP

NativePHP and the Microphone Permission Menace: Taming the One-Time Access Beast

Next up: the dreaded microphone permission showdown. Every time a caller tried to start a call, the ominous prompt blasted onto the screen—“Would Like to Access the Microphone.” Sure, having a working microphone is essential, but this relentless gatekeeper made the whole experience feel like navigating a booby-trapped jungle rather than a smooth voice app. Not exactly user-friendly, but hey, adventure comes with its challenges!

After diving into the wild unknown as a zero-skilled Swift explorer, I finally uncovered the treasure map: the solution! The key lay in adding this magic snippet to the nativephp/ios/nativePHP/ContentView.swift file — unlocking the path to seamless microphone access. Ready for the next step? Here’s the code that changed everything.

func addSwipeGestureSupport(context: Context) {
    webView.navigationDelegate = context.coordinator
    webView.uiDelegate = context.coordinator   // Assign UI delegate here

    let swipeLeft = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleSwipeLeft(_:)))
    swipeLeft.direction = .left
    webView.addGestureRecognizer(swipeLeft)

    let swipeRight = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleSwipeRight(_:)))
    swipeRight.direction = .right
    webView.addGestureRecognizer(swipeRight)
}

And at the end of the file

extension WebView.Coordinator: WKUIDelegate {
    func webView(_ webView: WKWebView,
                 requestMediaCapturePermissionFor origin: WKSecurityOrigin,
                 initiatedByFrame frame: WKFrameInfo,
                 type: WKMediaCaptureType,
                 decisionHandler: @escaping (WKPermissionDecision) -> Void) {

        // You can customize this to match your allowed domains
        if origin.host == "127.0.0.1" || origin.host == "localhost" {
            print("Auto-granting mic permission for host: \(origin.host ?? "unknown")")
            decisionHandler(.grant)
        } else {
            print("Denying mic permission for host: \(origin.host ?? "unknown")")
            decisionHandler(.deny)
        }
    }
}

This heroic tweak summoned a one-time-only “Would Like to Access the Microphone” message—no more pesky pop-ups haunting the user after that. Even better? The user can deny microphone permission anytime through the app settings, and the app still keeps running smoothly. Mission accomplished: microphone access, tamed and hassle-free!

To enable microphone usage add the following code to your Info.plist

<key>NSMicrophoneUsageDescription</key>
   <string>This app requires access to the microphone to record your voice.</string>

Leave a Reply

Your email address will not be published. Required fields are marked *