Skip to content

How to load Html File in WKWebView or SFSafariVC ? #371

Description

@theRealGupta

How to load HTML by string?

let server = HttpServer()
server[path] = { request in
    return HttpResponse.ok(.text("<html string>"))
}
server.start()

This code is mention in your Doc file.

Que 1: What is server[path] ? Means Path = ??? in place of path what path i will provide?
Que 2: After loading Html file in using HttpHandlers.directory(NSBundle.mainBundle().resourcePath! + "/www")
how i will display in our WKWebView or SFSafariVC ? what will be url for that?

Activity

  1. Vkt0r commented on Mar 27, 2019

    @Vkt0r
    Member

    Uhmm 🤔 I think we need to add more docs to the repository to explain the different uses of the library. I'm going to create a new issue to keep track of missing docs.

    For now @anupgupta-arg I'm going to explain to you each one of your cases in the best I can for now 😀:

    What is server[path]?

    In the example of the README is where you define your routes for example:

    server["/hello"] = { request in
        return HttpResponse.ok(.text("<html string>"))
    }

    By default, the server is going to run in localhost:8080 or any other port you specify when you started it. So the above definition of the route hello means when you post localhost:8080/hello in any browser including your Safari in your Mac or Simulator the response would be the specified in this case the <html string>.

    After loading the HTML file using HttpHandlers.directory(NSBundle.mainBundle().resourcePath! + "/www") how I will display in our WKWebView or SFSafariVC

    So let's say you want to display some HTML file as the response for a specific request and for the sake of simplicity let's use the same route as before /hello. So we want the server emits the HTML file we load from the Bundle so something like this would work:

    server["/hello"] = { request in
        let stringFile = // load your file here as a String file 
        return HttpResponse.ok(.text(stringFile))
    }

    But we have some public functions to handle some cases too:

    public func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {

    A very good file with examples can be found in the https://github.com/httpswift/swifter/blob/stable/Sources/DemoServer.swift

    I hope this can help you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions