Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local web server for html in webView #206

Closed
MicheleMola opened this issue Oct 25, 2016 · 15 comments
Closed

Local web server for html in webView #206

MicheleMola opened this issue Oct 25, 2016 · 15 comments

Comments

@MicheleMola
Copy link

I have a file .html that needs to run in a local web server, Inside a webView. How can I do? Thank you all.

@tim-9red7
Copy link

You should first start by reading the wkwebview documentation. You could share a directory of files like the documentation says on the front page.
let server = HttpServer() server["/desktop/:path"] = shareFilesFromDirectory("/Users/me/Desktop") server.start()

You could probably use NSBundle on macOS to share a folder from the app bundle (haven't tried that) or NSDocumentDirectory on iOS.

Or can you just create the html in the server itself and serve from there?

@MicheleMola
Copy link
Author

I have already read the wkwebview documentation. I want create a web server on iPhone that executes the html file. I have the html file in local(on iPhone). The html file must be launched on the web server, but I don't want to use connection to internet.

@damian-kolakowski
Copy link
Member

@MicheleMola Maybe this will work for you: http://stackoverflow.com/a/26202983/351747 ?

@git-no
Copy link

git-no commented Nov 8, 2016

@MicheleMola swifter does this. As @tim-9red7 wrote you have to point the http server (swifter) to a folder of your html files (often your bundle dir or application dir on the device).
For me it works fine. No internet connection required.

@MicheleMola
Copy link
Author

MicheleMola commented Nov 8, 2016

@glock45 @git-no @tim-9red7 Thank you, I solved in this way:

                let server = demoServer(Bundle.main.resourcePath!)
                server["/:path"] = shareFilesFromDirectory(self.path)
                try server.start(9080)           
                let myRequest = NSURLRequest(url: NSURL(string: "http://localhost:9080") as! URL)
                self.webView.loadRequest(myRequest as URLRequest)

Where self.path is the path of the index.hmtl file that I want to run in a local web server.

@StarLard
Copy link

@MicheleMola Did you have to modify the demoServer func at all to accommodate this?

@Rups1990
Copy link

@MicheleMola I have .exe of the server inside my app bundle, which needs to be started to load my.html (which is also bundled). Can I achieve the same using the above code? and also demoServer function call is not clear to me

@MicheleMola
Copy link
Author

Hi @Rups1990, where must you launch .exe file?

@Rups1990
Copy link

Rups1990 commented Jun 11, 2018

thanks @MicheleMola .that was not clear to me..I have to load 360 image using KRPano viewer. They say that, their local testing server is required to load .html page without any browser restriction issues. Hence that, .exe of testing server is autogenerated and will be available for each Panorama image to be loaded.

I have added that .html and other resources including my .exe file to application bundle.Not sure of how to use that .exe file to start the server

@MicheleMola
Copy link
Author

@Rups1990 Okay, I get it. Can you give me the package that you added to application bundle? To understand more.

@Rups1990
Copy link

equi05.zip

@theRealGupta
Copy link

Hi,

func openWebView2() {
        
        path = Bundle.main.path(forResource: "index", ofType: "html")
        let server = demoServer(Bundle.main.resourcePath!)
        server["/:path"] = shareFilesFromDirectory(self.path!)
        do {
            try server.start(9080)
            let myRequest = NSURLRequest(url: NSURL(string: "http://localhost:9080")! as URL)
            print("myRequest>>",myRequest)
            self.wkwebV.load(myRequest as URLRequest)
        }catch{
            print("erororor")
        }
        
    }

this is my code I am a getting

/var/containers/Bundle/Application/BC6BCD5D-3802-4608-888F-2731A2815F66/Swift Server Test.app
myRequest>> <NSURLRequest: 0x1c4005dd0> { URL: http://localhost:9080 }
Middleware: ::80ea:3c0:100:0 -> GET -> /

@dvhdr
Copy link

dvhdr commented Apr 12, 2019

Note that you'll need to add:

<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<true/>
	</dict>
</key>

...to you Info.plist, to get the app to allow you to load from your local server! You can (should) probably be more secure, eg. restrict it to localhost.

@YiYiZheng
Copy link

YiYiZheng commented Apr 13, 2019

let httpServer = demoServer(Bundle.main.resourcePath!)
httpServer["/:path"] = shareFilesFromDirectory(Bundle.main.path(forResource: "Web", ofType: nil)!)
let port = 9080

do {
    try httpServer.start(in_port_t(port))
} catch {
    printLog("error: \(error)")
    return
}
let myRequest = URLRequest(url: URL(string: "http://localhost:\(port)/index.html")!)
self.webView.load(myRequest)

status code is 404 in existing project, but works in new project. why??

@bgoncal
Copy link

bgoncal commented Aug 2, 2021

To me it is working perfectly, but if I have folders inside of folders they don't become available, so if my index.html uses like ... "scripts/script.js", this "script.js" is not visible.

Anyone know how to make it visible without moving it to the same level as index.html?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants