httpswift / swifter

Tiny http server engine written in Swift programming language.
BSD 3-Clause "New" or "Revised" License
3.89k stars 540 forks source link

Html Audio loading problem in WKWebview swift #481

Open Jaikarthick12 opened 3 years ago

Jaikarthick12 commented 3 years ago

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.

michaelenger commented 3 years ago

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
Jaikarthick12 commented 3 years ago

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

michaelenger commented 3 years ago

@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.

Jaikarthick12 commented 3 years ago
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.

michaelenger commented 3 years ago

@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?