Contents

WebPage

An object that controls and manages the behavior of interactive web content.

Declaration

@MainActor final class WebPage

Overview

A WebPage is an Observable type, which you use to access various properties of web content and track changes to them. Use WebPage to interact with web content, like evaluating JavaScript or converting the page to PDF data. The following example shows you how you can combine these capabilities to get specific metadata from an ephemeral page with a custom user agent:

func fetchMetadata(for url: URL) async throws -> (title: String, description: String) {
    let botAgent = """
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/601.2.4 (KHTML, like Gecko) Version/9.0.1 Safari/601.2.4 facebookexternalhit/1.1 Facebot Twitterbot/1.0
    """

    var configuration = WebPage.Configuration()
    configuration.loadsSubresources = false
    configuration.defaultNavigationPreferences.allowsContentJavaScript = false
    configuration.websiteDataStore = .nonPersistent()

    // Set up the configured page.

    let page = WebPage(configuration: configuration)
    page.customUserAgent = botAgent

    // Load the request and wait for navigation to complete.

    let request = URLRequest(url: url)
    for try await event in page.load(request) {
        // Optionally do something with `event`.
    }

    // At this point, the navigation is complete.
    // Now, use JavaScript to query the appropriate properties of the page.

    let fetchOpenGraphProperty = """
    const propertyValues = document.querySelectorAll(`meta[property="${property}"]`);
    return propertyValues[0];
    """

    let javaScriptResult = try await page.callJavaScript(fetchOpenGraphProperty, arguments: arguments)
    guard let description = javaScriptResult as? String else {
        // Handle failure, like throwing an error.
    }

    guard let title = page.title else {
        // Handle failure, like throwing an error.
    }

    return (title, description)
}

Use WebPage to programmatically navigate to various types of resources like URL requests, HTML strings, and data. Optionally, you can observe these navigations through the async sequence returned by their associated loading functions, and you can customize them by using a type that conforms to the WebPage.NavigationDeciding protocol. You can also use the backForwardList property to observe changes to people’s navigation history, and to programmatically navigate to a specific back-forward list item.

WebPage also conforms to the Transferable protocol. You can use this conformance to export the page to various different types of content, like PDF, web archive data, and other types. For customization of PDF or image exprt, use exported(as:).

Topics

Creating a WebPage

Managing navigation between webpages

Observing navigation between webpages

Configuring a WebPage

Loading web content

Managing the loading process

Inspecting page information

Executing JavaScript

Customizing JavaScript dialogs

Exporting webpage content

Interacting with media

Managing the microphone and camera

See Also

Essentials