iOS Settings

  1. Grant camera permission by adding NSCameraUsageDescription to your app’s Info.plist file.
    <key>NSCameraUsageDescription</key> <string>To capture document images for verification...</string>
  2. In your web view, enable allowsInlineMediaPlayback to allow the camera page to function correctly.
  3. Finally, you want to listen to events fired by us when the verification session completes and perform actions accordingly like hiding the webview or navigation to some other screen on your app.

Here's a full working example:

import SwiftUI
import WebKit

// Message handler that listens to events from the webview
class MessageHandler: NSObject, WKScriptMessageHandler {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "postMessageListener" {
            let messageBody = message.body as! String
            if messageBody == "AiPriseVerification:Success" {
                // User reached the last page of verification
            } else if messageBody == "AiPriseVerification:Complete" {
                // User clicked on the 'continue` button on the last page. Time to hide the webview!
            } else if messageBody.hasPrefix("AiPriseVerification:Error:") {
                // Happens when the session URL is invalid or the session is completed / expired.
                // Extract error code from the end of messageBody string and check.
            }
        }
    }
}

// WebView Wrapper
struct WebView: UIViewRepresentable {
    var url: URL
    
    func makeUIView(context: Context) -> WKWebView {
        // To listen to events fired from the webview
        let contentController = WKUserContentController()
        let messageHandler = MessageHandler()
        contentController.add(messageHandler, name: "postMessageListener")

      	// Create web view config and attach the event listners
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        webConfiguration.userContentController = contentController
        
        // Create and return the webview
        let webView = WKWebView(frame: .zero, configuration: webConfiguration)
        return webView
    }

    func updateUIView(_ uiView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        uiView.load(request)
    }
}

// Your page
struct ContentView: View {
    
    var body: some View {
        VStack {
            WebView(url: URL(string: "YOUR_SESSION_URL_HERE")!)
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}