Create and load an HTML file with the Quill editor and Codox initialization logic using a WKWebView
In Swift, fetch the initial document state from the backend and initialize the Quill editor.
In Swift, set up listeners (callbacks) to respond to Codox client library session and error events.
Explicitly start Codox session via a javascript interface.
When Codox triggers hooks in the JavaScript code, corresponding callbacks in Swift are invoked, passing the necessary data.
Step-by-Step Integration Tutorial
Step 1: Set Up Local Files
The first step is to prepare the local files for your project. Create seprate directory QuillJSEditor/ in project root to store html and javascript files
Navigate to created directory QuillJSEditor/ and create local html file:
Create a directory for Codox library files:
Navigate to codoxLib/ and create package.json file for javascript dependencies
Install the library:
Copy the Codox lib file into QuillJSEditor/ directory:
The QuillJSEditor/ directory should now look like this:
Step 2: Initialize QuillJS Editor
Add the following code to quill.html to set up the Quill editor:
Step 3: Initialize WKWebView in Swift
In swift app in ViewController.swift init WKWebView with local html with Quill editor load. To display the HTML/Javascript app in Swift, we will use the WKWebView class. The following code in ViewController.swift sets this up:
Step 4: Inject Codox library script
Local javascript file codox.js should be injected into WKWebView after local html page is loaded.
Inject Codox library script codox.js into WKWebView after local html page is loaded. The following code in ViewController.swift sets this up:
Step 5: Initialize the Editor State
Before initiating or joining a Codox session, the latest version of the document content must be available and must be setup in editor. The following code shows how to set up this from Swift app.
Add an initialization function in your HTML/Javascript quill.html to expose a method for Swift to set up the editor with initial content.
Then, invoke this function from Swift after the page has been fully loaded using the webView method from WKWebView .
Start Codox Session
Setup start session code in quill.html.
To start or joining a Codox session, you need to explicitly call codox.start to connect to the session.
Setup start session code in quill.html:
Then, invoke this function from Swift after the page has been fully loaded using the webView callback from WKWebView, in ViewController.swift . Note that starting the session proceeds after the initial document state has been passed into the editor.
Optional: Codox Hooks and Error events
To subscribe to codox hooks and error events, need to add related code both on javascript and swift sides.
In quill.html need to add hooks and error events callbacks to codox config. The callbacks contain code which sends data to swift
You can listen to events from Codox to allow the Swift app to respond to content change notifications, member change notifications, and errors triggered within the Codox session.
In quill.html, define callback functions and include them in the config object passed to Codox. These callbacks are triggered internally when specific events occur and serve as bridges to invoke corresponding handlers in the Swift app.
On the Swiftt side you need to setup handlers to manage JavaScript messages sent from Codox, like the following example in ViewController.swift: