Contents

WKScriptMessageHandlerWithReply

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

Declaration

@MainActor protocol WKScriptMessageHandlerWithReply : NSObjectProtocol

Overview

Adopt the WKScriptMessageHandlerWithReply protocol when your app needs to receive JavaScript messages from a web view and provide an appropriate response. When JavaScript code sends a message that specifically targets your message handler, WebKit calls your handler’s userContentController(_:didReceive:replyHandler:) method. Use that method to process the message and provide your response.

To call your message handler from JavaScript, send a message to window.webkit.messageHandlers.<messageHandlerName>.postMessage(<messageBody>) in your code. You specify the name of your message handler when you add it to a WKUserContentController object.

Topics

Receiving Messages

See Also

Adding and Removing Message Handlers