https://github.com/doo/scanbot-sdk-example-ios-webkit-native-bridge
https://github.com/doo/scanbot-sdk-example-ios-webkit-native-bridge
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/doo/scanbot-sdk-example-ios-webkit-native-bridge
- Owner: doo
- Created: 2019-10-09T09:49:57.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-09T10:56:52.000Z (over 6 years ago)
- Last Synced: 2025-01-25T08:11:56.082Z (over 1 year ago)
- Language: Swift
- Size: 14.6 KB
- Stars: 0
- Watchers: 13
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Scanbot SDK WKWebView Example
This example app shows a simple bridge implementation between the iOS `WKWebView` and the native [Scanbot SDK](https://scanbot.io/en/sdk).
## How to run this app
```
pod install
open ScanbotSdkWebKitNativeBridgeExample.xcworkspace
```
## Basics
### JavaScript message handlers
Register JavaScript message handlers when creating an instance of `WKWebView`:
```
let contentController = WKUserContentController();
contentController.add(self, name: "startDocumentScanner")
let webConfiguration = WKWebViewConfiguration()
webConfiguration.userContentController = contentController
webView = WKWebView(frame: .zero, configuration: webConfiguration)
```
Receive JavaScript messages:
```
extension ViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "startDocumentScanner", let options = message.body as? NSDictionary {
// Start the native Document Scanner ...
startDocumentScanner(options: options)
}
}
}
```
### Post messages from JavaScript - web to native
```
function startDocumentScanner() {
const options = { ... };
webkit.messageHandlers.startDocumentScanner.postMessage(options);
}
```
### Call JavaScript - native to web
```
let jsonPayload = "{ \"pages\": [{...}, {...}] }"
webView.evaluateJavaScript("documentScannerCallback("+jsonPayload+")", completionHandler: nil)
```
```
function documentScannerCallback(result) {
// result.pages[0] ...
}
```