Skip to content

Commit 259881d

Browse files
authored
Update README.md
1 parent d2364c4 commit 259881d

File tree

1 file changed

+15
-42
lines changed

1 file changed

+15
-42
lines changed

README.md

+15-42
Original file line numberDiff line numberDiff line change
@@ -35,56 +35,30 @@ Then, run pod install or pod update in your terminal.
3535

3636
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`:
3737

38+
- Import necessary modules and conform to WKScriptMessageHandler:
3839
```swift
3940
import UIKit
4041
import WebKit
4142
import BridgeKit
4243

4344
class ViewController: UIViewController, WKScriptMessageHandler {
44-
private var webView: WKWebView!
45-
private var bridge: BridgeKitCore!
46-
47-
override func viewDidLoad() {
48-
super.viewDidLoad()
49-
50-
let contentController = WKUserContentController()
51-
contentController.add(self, name: "bridgekit") // Important: Must match JS name
52-
53-
let config = WKWebViewConfiguration()
54-
config.userContentController = contentController
55-
56-
webView = WKWebView(frame: .zero, configuration: config)
57-
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
6053

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
6756
```
6857

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` protocol to 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` protocol to 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:
7059

7160
```swift
7261
private func registerHandlers() {
73-
// Handle "showAlert" message
74-
struct ShowAlertTopic: Topic {
75-
let name: String = "showAlert"
76-
}
77-
78-
struct EmptyData: Codable {} // Data type for showAlert (no data)
79-
80-
bridge.registerMessageHandler(for: ShowAlertTopic()) { [weak self] (_: 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)
83-
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
84-
self?.present(alert, animated: true, completion: nil)
85-
}
86-
}
87-
8862
// Handle "myTopic" message with string data
8963
struct ResponseData: Codable {
9064
let receivedText: String
@@ -100,12 +74,10 @@ Then, run pod install or pod update in your terminal.
10074
}
10175
```
10276

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):
10478

10579
```swift
10680
@objc private func sendTextToWeb() {
107-
guard let text = textField.text, !text.isEmpty else { return }
108-
10981
struct TextData: Codable {
11082
let text: String
11183
}
@@ -125,7 +97,7 @@ Then, run pod install or pod update in your terminal.
12597
}
12698
```
12799

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:
129101

130102
```html
131103
<!DOCTYPE html>
@@ -179,6 +151,7 @@ Then, run pod install or pod update in your terminal.
179151
</html>
180152
```
181153

154+
For more info please inspect example code.
182155
## Conclusion:
183156

184157
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

Comments
 (0)