Contents

WKScriptMessageHandler

An interface for receiving messages from JavaScript code running in a webpage.

Declaration

@MainActor protocol WKScriptMessageHandler : NSObjectProtocol

Overview

Adopt the WKScriptMessageHandler protocol when your app needs a way to respond to JavaScript messages in the web view. When JavaScript code sends a message that specifically targets your message handler, WebKit calls your handler’s userContentController(_:didReceive:) method. Use that method to implement your response. For example, you might update other parts of your app in response to web content changes.

To call your message handler from JavaScript, call the function window.webkit.messageHandlers.<messageHandlerName>.postMessage(<messageBody>) in your code. You specify the value of <messageHandlerName> when you install your message handler in a WKUserContentController object.

Topics

Receiving Messages

See Also

Adding and Removing Message Handlers