The JavaScript Plugin Container is a Swift-based library designed to facilitate communication between Swift applications and JavaScript code within WKWebViews. It allows for a structured and dynamic approach to handle JavaScript calls within Swift by leveraging a plugin architecture.
- Dynamic plugin management for JavaScript interfaces.
- Easy integration with WKWebView.
- Support for macOS, iOS, watchOS, and tvOS platforms.
- Debug utilities for plugin scanning and management.
- iOS 13.0+
- Swift 5.10
To integrate the JavaScript Plugin Container into your project, copy the provided .swift
files directly into your project or create a Swift Package:
You can add this library as a dependency in your Package.swift
file:
dependencies: [
.package(url: "https://github.com/minsOne/JSInterfacePluginContainer-iOS.git", from: "1.0.0")
]
Implement the JSInterfacePluggable
protocol with your custom plugin classes:
class MyPlugin: JSInterfacePlugin {
var action: String { "myAction" }
func callAsAction(_ message: [String: Any], with webView: WKWebView) {
// Handle the action
}
}
Create an instance of JSInterfaceSupervisor
and manage your plugins:
let supervisor = JSInterfaceSupervisor()
let myPlugin = MyPlugin()
supervisor.loadPlugin(myPlugin)
Use the resolve
function to handle incoming JavaScript calls:
supervisor.resolve("myAction", message: ["key": "value"], with: webView)
The library includes debugging aids that are active with the DEBUG flag. These aids can help identify registered plugins and ensure proper setup.
Contributions to the JavaScript Plugin Container are welcome. Here are ways you can contribute:
- Reporting issues
- Suggesting enhancements
- Submitting pull requests with bug fixes or new features
Please ensure to follow the coding standards and write tests for new functionality.
Specify your licensing details here. For open-source projects, an MIT License is a common choice.
- GitHub