> For the complete documentation index, see [llms.txt](https://docs.hello-lisa.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hello-lisa.com/developers/guides/integration-guide/app-integration/ios/communication.md).

# Communication

This guide explains how to establish seamless bidirectional communication between your iOS app and the LiSA LiSA Player running within a WebView. By leveraging native-to-JavaScript bridges and WebView event listeners, you can enable your Android app and the LiSA Player to exchange data, trigger events, and synchronize state in real time.

Whether you need to send commands from you app to the LiSA Player (e.g., changing app behavior or updating content) or pass data from the WebView back to your app (e.g., user actions or analytics), this guide will walk you through the necessary setup and best practices for efficient, secure, and reliable communication.

### Receiving Messages

To receive messages from the LiSA Player in your iOS application, use a **message handler** in `WKWebView`.&#x20;

Configure `WKWebView` to add a message handler:

```swift
import SwiftUI
import WebKit

struct WebView: UIViewRepresentable {
    // Define the specific UIView subclass we are representing
    typealias UIViewType = WKWebView

    let urlString: String

    func makeUIView(context: Context) -> WKWebView {
        // Configuration
        let webViewConfiguration = WKWebViewConfiguration()
        
        // ... shortened ...

        let contentController = WKUserContentController()

        // Register the message handler with the coordinator
        contentController.add(context.coordinator, name: "MessageFromLiSA")
        webViewConfiguration.userContentController = contentController

        // Create WKWebView
        // Use .zero frame; SwiftUI will manage the actual frame size
        let webView = WKWebView(frame: .zero, configuration: webViewConfiguration)

        // ... shortened ...

        // Return the WKWebView directly
        return webView
    }

    func updateUIView(_ webView: WKWebView, context: Context) {
        // The `uiView` parameter *is* the WKWebView because we set UIViewType
        // Check if the urlString prop has changed and reload if necessary
        if let currentURL = webView.url?.absoluteString, currentURL == urlString {
           // URL hasn't changed, do nothing
           return
        }
        
        // URL string has changed, load the new one
        if let url = URL(string: urlString) {
            let request = URLRequest(url: url)
            webView.load(request)
        }
    }

    func makeCoordinator() -> Coordinator {
        // Pass the WebView struct itself if needed
        Coordinator(self)
    }

    // Coordinator
    // Handles callbacks from the WKWebView
    class Coordinator: NSObject, WKScriptMessageHandler, WKNavigationDelegate, WKUIDelegate {
        // Reference back to the SwiftUI view
        var parent: WebView
        // Weak reference maybe better if lifecycle allows?
        var webView: WKWebView?

        init(_ parent: WebView) {
            self.parent = parent
        }

        // WKScriptMessageHandler
        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
            if message.name == "MessageFromLiSA" {
                // Handle message from JavaScript
                print("Received message from LiSA: \(message.body)")
                // You could trigger actions in your SwiftUI view here if needed,
                // e.g., using @State variables passed down or Combine publishers.
            }
        }
    }
}
```

* The message handler is registered under the name `MessageFromLiSA`.
* When the LiSA Player sends a message using `window.webkit.messageHandlers.MessageFromLiSA.postMessage`, the `didReceive` method in the `Coordinator` processes it.
* You can parse and handle the message (JSON) within the `didReceive` method.

***

### Sending Messages

To send messages from iOS to the LiSA Player, use the `evaluateJavaScript` method in your Swift code:

```swift
webView.evaluateJavaScript("""
    window.postMessage(JSON.stringify({ type: 'event', payload: { key: 'value' } }));
""", completionHandler: nil)

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.hello-lisa.com/developers/guides/integration-guide/app-integration/ios/communication.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
