<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.10.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "WebKit",
  "identifier" : "/documentation/WebKit/WKUserContentController/add(_:name:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "WebKit"
    ],
    "preciseIdentifier" : "c:objc(cs)WKUserContentController(im)addScriptMessageHandler:name:"
  },
  "title" : "add(_:name:)"
}
-->

# add(_:name:)

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

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

## Parameters

`scriptMessageHandler`

The message handler object that implements your custom code. This object must adopt the [`WKScriptMessageHandler`](/documentation/WebKit/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.`    *<name>*    `.postMessage(<messageBody>)`    , where     *<name>*     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`](/documentation/WebKit/WKContentWorld/page) property of [`WKContentWorld`](/documentation/WebKit/WKContentWorld).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)