Android Settings

  1. Allow camera and internet permissions in your AndroidManifest.xml
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    
  2. In your WebView enable JavaScript, file access and DOM storage and also disable mediaPlaybackRequiresUserGesture to allow the camera page to function properly.
    myWebView.settings.javaScriptEnabled = true
    myWebView.settings.allowFileAccess = true
    myWebView.settings.domStorageEnabled = true
    myWebView.settings.mediaPlaybackRequiresUserGesture = false
    
  3. Additonally, you will have to ask for runtime permissions when the WebView asks for it.
    myWebView.webChromeClient = object : WebChromeClient() {
        override fun onPermissionRequest(request: PermissionRequest) {
            if (request.resources.contains(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
                if (ContextCompat.checkSelfPermission(this@MyActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this@MyActivity, arrayOf(Manifest.permission.CAMERA), CAMERA_PERMISSION_REQUEST_CODE)
                } else {
                    request.grant(request.resources)
                }
            }
        }
    }
    
  4. 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. You can do that by creating a JavaScript interface for the webview:

Full Working Example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
package com.example.aipriseandroidtest

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Bundle
import android.webkit.JavascriptInterface
import android.webkit.PermissionRequest
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

// JS Interface to listen to events from the WebView
class WebAppInterface(private val context: Context) {
    @JavascriptInterface
    fun postMessage(message: String) {
        // Handle the message here
        if (message == "AiPriseVerification:Success") {
            // User reached the last page of verification
        } else if (message == "AiPriseVerification:Complete") {
            // User clicked on the 'continue` button on the last page. Time to hide the webview!
        } else if (message.startsWith("AiPriseVerification:Error:")) {
            // Happens when the session URL is invalid or the session is completed / expired.
            // Extract error code from the end of message string and check.
        }
    }
}

// Your activity...
class MyActivity : AppCompatActivity() {

    private lateinit var myWebView: WebView
    private var currentPermissionRequest: PermissionRequest? = null
    private val CAMERA_PERMISSION_REQUEST_CODE = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my);

        myWebView = findViewById(R.id.webview)

        myWebView.webViewClient = WebViewClient()
        myWebView.webChromeClient = object : WebChromeClient() {
            override fun onPermissionRequest(request: PermissionRequest) {
                if (request.resources.contains(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
                    if (ContextCompat.checkSelfPermission(this@MyActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        currentPermissionRequest = request
                        ActivityCompat.requestPermissions(this@MyActivity, arrayOf(Manifest.permission.CAMERA), CAMERA_PERMISSION_REQUEST_CODE)
                    } else {
                        request.grant(request.resources)
                    }
                }
            }
        }

        // Update webview settings
        myWebView.settings.javaScriptEnabled = true
        myWebView.settings.allowFileAccess = true
        myWebView.settings.domStorageEnabled = true
        myWebView.settings.mediaPlaybackRequiresUserGesture = false
      
        // Attach the interface to your webview to listen to events
        myWebView.addJavascriptInterface(WebAppInterface(this), "Android")

        // Load the URL returned by our API
        myWebView.loadUrl("YOUR_SESSION_URL_HERE")
    }

    // Handle response from permission request
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                currentPermissionRequest?.grant(currentPermissionRequest?.resources)
            } else {
                currentPermissionRequest?.deny()
            }
            currentPermissionRequest = null
        }
    }
}