Developers attach path handlers to the Web Frame for handling web view requests. Path handlers are usually used for providing assets and resources to the web view.

Path handlers must adhere to the PathHandler protocol. One particularly useful implementation is InternalStoragePathHandler, which serves static files from almost any directory on the native device.

Examples

In the following example, the /photos/ path is being registered and linked to a directory on the native device.

let documentsURL = try FileManager.default.url(
    for: .documentDirectory,
    in: .userDomainMask,
    appropriateFor: nil,
    create: true
)

let photosURL = documentsURL.appendingPathComponent("photos")

let configuration = WebFrame.Configuration(
    baseURL: ...
    functions: ...
    observables: ...
    pathHandlers: [
        "/photos/": InternalStoragePathHandler(directory: photosURL)
    ]
)

Swift API

InternalStoragePathHandler

A path handler which serves static files from a directory.

This class is implemented in Peregrine to achieve partial parity with the first-party path handlers available on Android. It is largely inspired by WebViewAssetLoader.InternalStoragePathHandler.

Initializers

init(directory: URL)

Create an InternalStoragePathHandler bound to a specific directory.

PathHandler

A protocol for serving data from paths.

Implementers must have a handle method which returns a PathHandlerResponse.

public protocol PathHandler {
    func handle(path: String) -> PathHandlerResponse
}

PathHandlerResponse

Represents a single response from a path handler.

Responses have an HTTP status code, HTTP headers, and data.

public struct PathHandlerResponse {
    let statusCode: Int
    let responseHeaders: [String: String]
    let data: Data
}