Device APIs
JavaScript that opens your camera. Legally.
▶ the lesson · narrated sketch video · tap for sound
the intro
Devices offer capabilities web code never had: camera, GPS, storage, sensors, notifications. React Native exposes them through libraries, and even the browser has grown a set of its own. The rule: ask for permission with a reason, at the moment you need it.
fun fact ✦
The Vibration API exists in web browsers. Your website can buzz. Use this power responsibly.
the docs
the deep dive
the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid
The camera, GPS, and accelerometer never belonged to your code — the OS owns the hardware and rents it out, one permission dialog at a time. Every device API therefore has two halves: the capability (read the sensor, open the lens) and the social contract (ask clearly, at the moment of use, and cope gracefully with no). Get the second half right and permissions become a feature, not a lawsuit.
the hardware is rented, not owned
key terms
secure context
HTTPS is the entry fee for camera, GPS, and share APIs. Localhost is exempt for development.
https:// or nothingruntime permission
Asked while the app runs, not at install. Users can grant, refuse, or revoke later.
"Allow access to camera?"feature detection
Check that the API exists before calling it, so the same code runs on laptop and phone.
if (navigator.share) { ... }graceful degradation
The app stays useful with permission denied: pick from the gallery instead of the camera.
no GPS? let users type a cityhow it works
- 1Detect the capability
Feature-check the API and show a working UI only when the device can actually do it.
- 2Explain the why, first
Put the reason in your own UI — "to scan your receipt" — before any system dialog appears.
- 3Ask at the moment of use
Request when the user taps the feature, never on first launch. Context earns the yes.
- 4Handle the no
If the status is denied, degrade: gallery picker, manual entry, a link to Settings.
- 5Mind the background
Background time is rationed and battery-aware. Fetch small, run brief, expect interruption.
the code, explained
the ask, in Expo form — one call, one native dialog
side by side
the web browser
- camera, GPS, share, vibration
- no install, works from a URL
- one permission dialog per API
- background access: barely any
native modules
- the full hardware surface
- real background tasks
- sensor data at full rate
- needs a store + native build
A refused permission is sticky. On iOS you cannot re-ask from code — the user must dig into Settings themselves, and most never will. That is why the in-app explanation comes before the dialog: you get exactly one clean shot at the ask.
quick answers
Can a website really open my camera?
Yes: getUserMedia over HTTPS, with a permission prompt. The stream stays on the page until you upload or send it somewhere.
Why does it work on localhost but not my LAN IP?
Secure contexts. Localhost is always trusted; any other host — including 192.168.x.x — needs HTTPS before camera or GPS APIs unlock.
Can web apps do real background work?
Barely. Push messages and short sync windows exist, but native apps get far more background time. Design web features to finish in the foreground.
quick hits about Device APIs
short answers · interviews, tips, trivia & hidden gems