Web Frames are instances of the WebFrame class in Swift. Essentially, they are meant to represent a single, supercharged web view. Web Frames are set up specifically for the web content they display: web view configuration and native access are tailored. Many hybrid developers may only use one Web Frame for their entire app—and that’s fine! Others may want to have a mix of web and native views.

Web Frames encapsulate the web view itself, path handlers for implementing responses to web view requests, and a remote interface for implementing native functionality.

Examples

🚧

Under Construction

Swift API

WebFrame

An instance of the WebFrame class represents a single native view with web content, as well as any configuration and native access associated with that web content.

Initializers

init(configuration: Configuration)

Properties

let view: WebFrameRepresentable

The UIViewRepresentable that can be plugged into SwiftUI.

In the following example, we use the view directly as the root view of our scene.

import SwiftUI
import Peregrine

@main
struct MyApp: App {
    let frame: WebFrame

    init() {
        let baseURL = Bundle.main.url(forResource: "www", withExtension: nil)!
        let configuration = WebFrame.Configuration(baseURL: baseURL)
        frame = WebFrame(configuration: configuration)
    }

    var body: some Scene {
        WindowGroup {
            frame.view
        }
    }
}

WebFrame.Configuration

A single Web Frame configuration which can be used for zero to many Web Frames.

Web Frame configurations contain the following.

  1. A baseURL which corresponds to the directory path from which to serve web content.

    This directory must have at least an index.html file.

  2. (optional) A remote interface, which comprises functions and observables.

    Functions are used to invoke something, e.g. run a native API, send back data, be notified of a particular lifecycle event, etc. Observables are used to receive a stream of notifications, e.g. network status, geofencing updates, keyboard events, etc.

    Each function and observable must have a unique string key for the web client’s usage. The string keys for observables must end in a dollar sign ($).

  3. (optional) Path handlers, which use special paths that are accessed by the web view to serve web content from Swift. See Path Handlers for reference documentation.

Initializers

Create a WebFrame.Configuration.

init(
    baseURL: URL?,
    functions: RemoteFunctions?,
    observables: RemoteObservables?,
    pathHandlers: [String: PathHandler]?
)

RemoteFunctions

typealias RemoteFunctions = [String: RemoteFunction]

RemoteFunction

typealias RemoteFunction = (_: Call) -> Void

RemoteObservables

typealias RemoteObservables = [String: RemoteObservable]

RemoteObservable

typealias RemoteObservable = AnyPublisher<Event?, Never>