Camera not working in PWA after app returns from background in iOS
See original GitHub issueDescribe the bug The camera stops working after the PWA returns form background on my 12 mini. The camera also doesn’t work at all on an iPhone 13 (see videos below). App works perfectly fine in Safari. PWA also works fine on Android devices.
To Reproduce Implement this Demo as PWA: https://gruhn.github.io/vue-qrcode-reader/demos/DecodeAll.html#decode-continuously
<template>
<div class="scanner">
<p class="error">{{ error }}</p>
<p class="decode-result">Last result: <b>{{ error }}</b></p>
<qrcode-stream @decode="onDecode" @init="onInit" />
</div>
</template>
<script>
import { QrcodeStream } from 'vue-qrcode-reader'
export default {
components: { QrcodeStream },
data () {
return {
result: '',
error: ''
}
},
methods: {
onDecode (result) {
this.result = result
},
async onInit (promise) {
try {
await promise
} catch (error) {
if (error.name === 'NotAllowedError') {
this.error = "ERROR: you need to grant camera access permission"
} else if (error.name === 'NotFoundError') {
this.error = "ERROR: no camera on this device"
} else if (error.name === 'NotSupportedError') {
this.error = "ERROR: secure context required (HTTPS, localhost)"
} else if (error.name === 'NotReadableError') {
this.error = "ERROR: is the camera already in use?"
} else if (error.name === 'OverconstrainedError') {
this.error = "ERROR: installed cameras are not suitable"
} else if (error.name === 'StreamApiNotSupportedError') {
this.error = "ERROR: Stream API is not supported in this browser"
} else if (error.name === 'InsecureContextError') {
this.error = 'ERROR: Camera access is only permitted in secure context. Use HTTPS or localhost rather than HTTP.';
} else {
this.error = `ERROR: Camera error (${error.name})`;
}
}
}
}
}
</script>
<style scoped>
.error {
font-weight: bold;
color: red;
}
.scanner {
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
left: 0;
top: 0;
}
</style>
Screenshots iPhone 12 mini behaviour
iPhone 13 behaviour
Smartphone:
- Devices: iPhone 12 mini, iPhone 13
- OS: iOS 15.4.1
- Browser: PWA
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:5
Top Results From Across the Web
Camera doesn't work in iOS PWA mode #76 - GitHub
The only issue I see is a hang/black camera view when the app goes to background then comes back again via tapping home...
Read more >Reactjs PWA - iOS on opening Progressive Web App Second ...
3) If I move out of and back into the app, the camera is black and cannot take a photo. 4) If I...
Read more >[Solved]-IOS camera returns black screen after leaving pwa
Recently I faced the same problem, the only solution I came up with was to open in the app in browser instead of...
Read more >camera does not open from iOS PWA | Apple Developer Forums
I have written a PWA which installs an icon when it loads first.The functionalites include offline storage using indexed db and camera.usage to...
Read more >iOS PWA Compatibility - firt.dev
High-level PWA support #. Ability, Supported, Since version. Offline Support with Service Workers, ✓, 11.3. App Installation from Browser ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I doubt that it is the reason. The devices are online the whole time. It just occurs when moving the app into background and back forward. It also behaves differently on different iOS devices (with same iOS-Version), so i would exclude the internet connection and therefore also the cdn as the reason for this.
@0tsu0i0ihajime the error is still not resolved and also the reason has not be found yet.
On an iPhone 12 I was testing on, the PWA app would get in a state where the video element of the reader would refuse to start at all after going into the background and coming back. My eventual workaround had two parts:
In
mounted()
of the component showing the qr-reader, find the video element of the qr-reader (usinggetElementsByTagName
or whatever), subscribe to theended
event of the video - which seems to fire when the badness happens. Use$router.back()
to close the component whenever this fires (this may or may not be appropriate depending on your implementation of the reader!)Additionally, in the
beforeDestroy
hook (Vue 2) of the component with the reader, find the video element and do this:I was starting to wonder about brushing off my Swift skills, such as they are, and going native at about this point (which presumably is exactly what Apple wants me to do!)