class ViewController: UIViewController, WKUIDelegate, WKScriptMessageHandler {
/// Assuming that the javascript sends message back, this function handles the message
/// - userContentController: controller
/// - message: Message. Can be a String or [String:Any] to a single level.
func userContentController(
_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage
print("something recived");
let messageBody = message.body as! [String: Any];
let action = messageBody["action"] as! String;
print("barcode was clicked");
lazy var webView: WKWebView = {
let webCfg:WKWebViewConfiguration = WKWebViewConfiguration()
// Setup WKUserContentController instance for injecting user script
var userController:WKUserContentController = WKUserContentController()
// Get the contents of the file `inject.js`
if let filePath:String = Bundle.main.path(forResource: "inject", ofType:"js") {
script = try! String(contentsOfFile: filePath, encoding: .utf8)
let userScript:WKUserScript = WKUserScript(source: script!, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: false)
userController.addUserScript(userScript)
// Add a script message handler for receiving "nativeProcess" event notifications posted from the JS document using window.webkit.messageHandlers.nativeProcess.postMessage script message
userController.add(self, name: "nativeProcess")
// Configure the WKWebViewConfiguration instance with the WKUserContentController
webCfg.userContentController = userController;
width: self.view.frame.width,
height: self.view.frame.height
override func viewDidLoad() {
// Do any additional setup after loading the view.
self.navigationItem.title = "ScandiPWA"
self.view.addSubview(webView)
let urlToLoad = URL(string: "https://tech-demo.scandipwa.com")
// Do any additional setup after loading the view.
webView.load(URLRequest(url: urlToLoad!))