This repo shows a basic POC for running Microblink's BlinkID using SwiftUI and WebView
- The application only consists of one view (WebView)
- Loads the web demo site https://blinkid-test.netlify.app/ into the browser
- Asks for camera permission when entering the scanning process and is making sure that scanning and extraction work
In order for the WebView to work, there are a few items that need to be addressed first.
-
Add the NSCameraUsageDescription to your Info.plist file. This is important as the camera scanning will not work if the end-user declines the alert pop-up.
-
Add the additional permissions when setting up WebView:
webViewConfiguration.userContentController.add(context.coordinator, name: "mediaCapture")
webViewConfiguration.allowsInlineMediaPlayback = true
webViewConfiguration.allowsPictureInPictureMediaPlayback = true
webViewConfiguration.allowsAirPlayForMediaPlayback = true
let webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
- Display the camera permission alert when the custom mediaCapture handler is triggered:
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "mediaCapture" {
requestCameraPermission()
}
}
private func requestCameraPermission() {
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
print("Camera access granted.")
} else {
print("Camera access denied.")
}
}
}
Hope this helps, and if you'll have any additional questions, please reach out to us and we'll be happy to help.