swifter icon indicating copy to clipboard operation
swifter copied to clipboard

Html Audio loading problem in WKWebview swift

Open Jaikarthick12 opened this issue 4 years ago • 5 comments

Am trying to load a local html file which takes input from images & audio folders, In native audio files are not loading inside the WKWebview . So we started a local server .when I visit the port inside safari it shows blank page, not able to find the solution even though we run a local server. Is it a path issue or something else?

let httpServer = demoServer(filepath.path ) httpServer["/:path"] = shareFilesFromDirectory(filepath.path ) do { try httpServer.start(8086) let myRequest = NSURLRequest(url: NSURL(string: "http://localhost:8086/testing.html")! as URL) webView.load(myRequest as URLRequest) }catch{ }

Any sample code will be much appreciated.

Jaikarthick12 avatar Feb 16 '21 15:02 Jaikarthick12

In my experience the WKWebview does not like loading from localhost unless you explicitly allow it. Could you try using http://0.0.0.0:8086/testing.html instead?

Also you can attach a WKNavigationDelegate to the web view to see any navigation errors:

class MyNavigationDelegate: NSObject, WKNavigationDelegate {
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("NAVIGATING") // just to make sure the delegate is working
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print(error)
    }
}

let navigationDelegate = MyNavigationDelegate()
webView.navigationDelegate = navigationDelegate // note: weak reference

michaelenger avatar Feb 25 '21 13:02 michaelenger

@michaelenger thanks a lot , I have tried but delegates doesn't throw any error logs.

Jaikarthick12 avatar Feb 25 '21 14:02 Jaikarthick12

@Jaikarthick12 That's strange. I'm not sure what context you're trying to run the server in but I threw together this SwiftUI view which runs the server and contains a WKWebView: https://gist.github.com/michaelenger/fd394865295320645ed0d1ded9675654

I'm not 100% sure how shareFilesFromDirectory would handle being on a iOS device, but the example in my gist works for me when I'm running the simulator.

michaelenger avatar Feb 25 '21 17:02 michaelenger

Screenshot 2021-02-26 at 11 09 44 AM I have shared you the files image . offline_preview.html will loads all these js,audio,template files , those paths are mentioned inside the offline_preview.html file .So the problem is with audio alone . when I tried load a single audio file its working fine without any issues but when I tried load a multiple files cant able see any audio files loaded.

Jaikarthick12 avatar Feb 26 '21 05:02 Jaikarthick12

@Jaikarthick12 I have to admit that I don't know how to help you without more context. Are there any error messages? Are you able to load multiple audio files if you open the offline_preview.html file in Safari? How are you running the server?

michaelenger avatar Feb 26 '21 08:02 michaelenger