world.askFor(features)
Asks the person in the webapp to let your view use the camera, the microphone or another browser feature that needs their consent.
askFor(features: BrowserFeature[]): Promise<true>
js
if (!world.webapp.allowed.includes('camera')) await world.askFor(['camera']);Parameters
| Parameter | What it is |
|---|---|
features | An array of BrowserFeature names chosen among 'camera', 'microphone', 'geolocation', 'clipboard-read', 'midi', 'usb', 'serial', 'hid', 'bluetooth', 'idle-detection' and 'local-fonts'. Other names are ignored. |
Returns
A Promise that resolves to true:
- at once when every requested feature is already in
webapp.allowedor when none of the names is in the list above - after the person accepts in every other case. A moment later the webapp reloads your page with the new features listed in
world.webapp.allowed.
When the person refuses, the promise rejects with the code 'declined'.
Usage
Ask on a click and then use the camera
Call askFor when the person asks for the feature (for example with a click on "Use my camera") rather than when the page starts. Check allowed on start as well because the page reloads after the person accepts:
js
async function startCamera() {
if (!world.webapp.allowed.includes('camera')) {
await world.askFor(['camera']); // with a yes, your page is loaded again
return;
}
// Allowed: the browser now asks the person as on any site, the first time.
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
video.srcObject = stream;
}
if (world.webapp.allowed.includes('camera')) startCamera(); // after the reload
cameraButton.onclick = () => startCamera().catch(showNoCamera);Ask for several at once
One call shows the person a single question for all the features:
js
await world.askFor(['camera', 'microphone']);Caveats
- After the person accepts, the webapp reloads your page at the last path you passed to
setPath. Keep the state of the person in the address or inlocalStorageand do not rely on code afterawait world.askFor(...). - As on any site, the browser asks too the first time your page uses the feature. The webapp asks first and names your site because the browser prompt shows the UNaIVERSE site instead of yours.
- A refusal lasts until the webapp loads your view again. Until then a new request for a refused feature rejects at once with
'declined'and shows no question. Offer an alternative that works without the feature. - A consent is kept on that device for the address of your view until the person forgets the world in the webapp.
- One question at a time. A call made while the person is answering another question waits until that question closes.
- Only these features need it. Every other feature, from fullscreen to downloads, is always available in your view (Everything a web page can do).
- With a fake world every feature counts as allowed and
askForresolvestrueat once without a reload. Passingwebapp: { allowed: [] }toconnectToHostshows your view as if none of these features were allowed.askForthen still resolvestrueand changes nothing.
Troubleshooting
| Code | Why | What to do |
|---|---|---|
'declined' | The person refused now or in an earlier request made after the webapp loaded your view. | Do not ask again. Offer an alternative without the feature (for example a photo sent from a file). |
'unavailable' | This webapp cannot let your view use these features. | Offer an alternative without the feature. |
'not-allowed-verb' | This webapp is older than your kit and does not know askFor. | Offer an alternative without the feature. |
js
try {
await world.askFor(['camera']);
} catch (error) {
if (error.code === 'declined') showUploadInstead();
else showNoCamera();
}