You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+15-42
Original file line number
Diff line number
Diff line change
@@ -35,56 +35,30 @@ Then, run pod install or pod update in your terminal.
35
35
36
36
2. **Initialize BridgeKit and Configure WKWebView:** In your view controller (typically in `viewDidLoad()`), create a `WKWebView`, configure its `WKUserContentController`, and initialize an instance of `BridgeKitCore`:
37
37
38
+
- Import necessary modules and conform to WKScriptMessageHandler:
view.addSubview(webView) // Add webview to your view hierarchy
58
-
59
-
bridge =BridgeKitCore(webView: webView)
45
+
```
46
+
47
+
- Configure the WKWebView and initialize BridgeKitCore:
48
+
```swift
49
+
let contentController =WKUserContentController()
50
+
contentController.add(self, name: "bridgekit") // Important: Must match JS name
51
+
let config =WKWebViewConfiguration()
52
+
config.userContentController= contentController
60
53
61
-
registerHandlers() // Register message handlers
62
-
loadWebViewContent() // Load your web content
63
-
setupUI() // Set up your UI (if needed)
64
-
}
65
-
// ...
66
-
}
54
+
webView =WKWebView(frame: .zero, configuration: config) // Give config to your webview
55
+
bridge =BridgeKitCore(webView: webView) // Inform BridgeKit about webview
67
56
```
68
57
69
-
3. **Register Message Handlers (Swift):** Define message handlers in your Swift code to respond to messages from JavaScript. Use structs conforming to the `Topic` protocolto define message topics, and `Codable` structs to define the data being sent. It's crucial to match the data structure with the one from js:
58
+
4. **Register Message Handlers (Swift):** Define message handlers in your Swift code to respond to messages from JavaScript. Use structs conforming to the `Topic` protocolto define message topics, and `Codable` structs to define the data being sent. It's crucial to match the data structure with the one from js:
70
59
71
60
```swift
72
61
private func registerHandlers() {
73
-
// Handle "showAlert" message
74
-
structShowAlertTopic: Topic {
75
-
let name: String="showAlert"
76
-
}
77
-
78
-
structEmptyData: Codable {} // Data type for showAlert (no data)
79
-
80
-
bridge.registerMessageHandler(for: ShowAlertTopic()) { [weakself] (_: EmptyData, bridge) in
81
-
DispatchQueue.main.async { // Ensure UI updates on main thread
82
-
let alert =UIAlertController(title: "Native Alert", message: "Button pressed in webview!", preferredStyle: .alert)
@@ -100,12 +74,10 @@ Then, run pod install or pod update in your terminal.
100
74
}
101
75
```
102
76
103
-
4. **Send Messages from Swift:** Use `bridge.postMessage` to send messages to your JavaScript code. Provide the data (as a `Codable` struct) and the topic (as a struct conforming to the `Topic` protocol):
77
+
5. **Send Messages from Swift:** Use `bridge.postMessage` to send messages to your JavaScript code. Provide the data (as a `Codable` struct) and the topic (as a struct conforming to the `Topic` protocol):
104
78
105
79
```swift
106
80
@objcprivatefuncsendTextToWeb() {
107
-
guardlet text = textField.text, !text.isEmptyelse { return }
108
-
109
81
structTextData: Codable {
110
82
let text: String
111
83
}
@@ -125,7 +97,7 @@ Then, run pod install or pod update in your terminal.
125
97
}
126
98
```
127
99
128
-
5. **JavaScript Code (index.html):** In your web page, use `window.webkit.messageHandlers.bridgekit.postMessage` to send messages to Swift. The `topic` and the structure of the `data` object must match the Swift `Topic` and `Codable` structs:
100
+
6. **JavaScript Code (index.html):** In your web page, use `window.webkit.messageHandlers.bridgekit.postMessage` to send messages to Swift. The `topic` and the structure of the `data` object must match the Swift `Topic` and `Codable` structs:
129
101
130
102
```html
131
103
<!DOCTYPE html>
@@ -179,6 +151,7 @@ Then, run pod install or pod update in your terminal.
179
151
</html>
180
152
```
181
153
154
+
For more info please inspect example code.
182
155
## Conclusion:
183
156
184
157
BridgeKit empowers you to seamlessly bridge the gap between your native app and webview content. With its intuitive API and robust features, it streamlines message handling, simplifies data exchange, and ultimately enhances your app's development experience.
0 commit comments