Contents

add(_:name:)

Installs a message handler that you can call from your JavaScript code.

Declaration

func add(_ scriptMessageHandler: any WKScriptMessageHandler, name: String)

Parameters

  • scriptMessageHandler:

    The message handler object that implements your custom code. This object must adopt the Wkscriptmessagehandler protocol.

  • name:

    The name of the message handler. This parameter must be unique within the user content controller and must not be an empty string.

    The user content controller uses this parameter to define a JavaScript function for your message handler in the page’s main content world. The name of this function is window.webkit.messageHandlers..postMessage(<messageBody>), where corresponds to the value of this parameter. For example, if you specify the string MyFunction, the user content controller defines the window.webkit.messageHandlers.MyFunction.postMessage() function in JavaScript.

Discussion

To execute your handler’s code, call the JavaScript function that this method defines. You may pass a parameter value to the method. The user content controller packages that value into an appropriate type and delivers it to your content handler’s delegate method.

This method uses the content world from the page property of WKContentWorld.

See Also

Adding and Removing Message Handlers