QR Code integration in PWA and its challenges 
Tags: pwa, qrcode, challenge, angular
Published: 2020-01-30 09:45:41

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We were developing a PWA app that needs us to scan QR codes if user in order to validate them. Now we knew how to integrate QR scanner in javascript but it was our first time PWA and we were clueless how we would achieve that in PWA. 

**Our Need?**

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We needed to implement QR code scanner functionality to confirm that users met with each other. Where one user shows QR code in their app and the other user needs to scan that QR code from the app. 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To implement this we needed two things 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QR CODE Generator (Which encode data)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QR CODE Scanner (Which decode data)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We went through the multiple libraries and amongst them, we used the following libraries. 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[angularx-qrcode](https://www.npmjs.com/package/angularx-qrcode) to generate QR Code.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ngx-scanner](https://github.com/zxing-js/ngx-scanner) to scan generated QR Code.

**NOTE:** Many times the scanner library does not scan QRs of all libraries. So you need to select a library accordingly.

**Where we got stuck?**

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Once we got the requirement we decided to make POC for that. We followed the steps given in the library documentation and created a nice demo where we generate QR code with required encrypted data which was decode by the integrated QR scanner. We tested this after adding an app to the home screen and it worked.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Unfortunately we tested this in Android device and not in an IOS device. We knew IOS PWA supports camera features but were unaware that it does not support streaming in camera. Hence the QR scanning did not work in IOS PWA. 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We had committed to deliver this feature to our client as we had tested the POC

**Our workaround.**

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We were able to catch and handle errors generated while the user tried to scan QR code in and IOS device. So we decided to handle this by checking QR supportiveness of the devices before the user reached that screen. If the user is on an unsupported device we would not show the user scan button, instead of that we show the user an OTP screen that they can ask to other users. The OTP screen for other users has a toggle switch between the QR code and the OTP screen to manage the flow accordingly.   


![PWA QR CODE](https://trt-react-images.s3-us-west-2.amazonaws.com/iphonePWD.jpg)

**How should you start?**

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1) First understand the requirements clearly. Make small  POC or demo before committing anything in PWA as there are too many things that are still not supported in IOS devices.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2) Find out a workaround that fulfils the system requirement without bothering users.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3) Select libraries whose community is more active and supports the latest version of the technologies being used.