iOS 12 Safari: Is there a way to make web-based QR Scanning work?
Asked Answered
A

2

5

There are a few working examples of web-based QR scanners out there, notably Instascan (repo) and JsQRScanner (repo). Both of these work flawlessly on Android. On Safari with iOS 12 on an iPhone however, both the examples and my code fails horrendously, producing a black box where the video feed should be or nothing at all. I have no way to debug this without a mac, since I can't catch javascript promises if I have no access to the promise object.

I tried both of these libraries and I even communicated with one of the developers, but logs would simplify the process. I can't produce these logs without proper tools on Safari.

Is there any way to make things work properly on Safari?

Here's a part of my JsQRScanner code. I also kept my old InstaScan code, in case anyone needs it.

/**
 * Sets up the QR scanner.
 * this function will be called when JsQRScanner is ready to use
 * @see https://github.com/jbialobr/JsQRScanner
 */
function JsQRScannerReady() {
    try {
        //create a new scanner passing to it a callback function that will be invoked when
        //the scanner succesfully scan a QR code
        var jbScanner = new JsQRScanner(scanEvent);
        setResult("Constructed JsQRScanner object.");
        //reduce the size of analyzed images to increase performance on mobile devices
        jbScanner.setSnapImageMaxSize(300);
        setResult("setSnapImageMaxSize completed.");
        var scannerParentElement = document.getElementById("videoBoundingBox");
        if (scannerParentElement) {
            //append the jbScanner to an existing DOM element
            jbScanner.appendTo(scannerParentElement);
            setResult("Appended jbScanner to div.");
        }   
    } catch (e) {
        setResult("Caught exception in the camera initialisation.");
        setResult(e.message);
    }
    setResult("initialisation complete.");
}
Annoying answered 20/9, 2018 at 12:27 Comment(1)
Hmm, I just noticed that this barcode scanner works, at least in Safari itself, as long as it's not in a PWA. github.com/code-kotis/barcode-scannerAnnoying
A
6

After an extensive conversation with the developer of JsQRScanner, we were able to make QR Scanning work on Safari.

Annoying answered 21/9, 2018 at 8:48 Comment(0)
R
0

The black box described in the question suggests that this problem is solved by the answers on this Stackoverflow question:

Accessing the camera in iOS Safari on an iPhone

The proposed fix there is to add this to the video element:

// Fix for iOS Safari from https://leemartin.dev/hello-webrtc-on-safari-11-e8bcb5335295
video.setAttribute('autoplay', '');
video.setAttribute('muted', '');

video.setAttribute('playsinline', '')

Relevant answered 14/4, 2023 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.