Permissions

permissions asks the user for the camera, the microphone, the location, speech recognition or notifications, says what they answered, and opens the app's settings page after a refusal. The camera views, notifications.schedule, Push.register() and the plugins ask on their own; use it to ask earlier or to handle a no. Picking a photo (openFilePicker) needs none.

  • ask for the camera or the microphone before a feature needs it (permissions.request)
  • check whether a permission is already granted, without a dialog (permissions.query)
  • explain why before the system dialog shows
  • the user denied a permission: send them to the app's settings (permissions.openSettings)
  • know when iOS, Android and the web ask again after a refusal
  • declare the permissions a published app asks for (Info.plist, Android manifest)

Platforms: every host; a dialog on iOS, Android, the web and the web player — on the desktop and in headless tests there is none and every kind answers "granted"; openSettings does nothing on the web, in the web player or on the desktop.

At a glance

TypeScript
const hint = UIColumn()

const record = async () => {
  const state = await permissions.request('microphone')   // the OS dialog the first time, the answer after
  if (state === 'granted') {
    hint.setContent([UIText('Recording…')])               // start the recorder here
  } else {
    hint.setContent([
      UIText('The microphone is off for this app'),
      UIButton(UIText('Open Settings')).onClick(() => permissions.openSettings()),
    ])
  }
}

UIScreen(UIButton(UIText('Record')).onClick(record), hint).open()

Ask for a permission: permissions.request

TypeScript
permissions.request(kind): Promise<PermissionState>   // the dialog when never asked; the state after

type PermissionKind = 'camera' | 'microphone' | 'location' | 'speech' | 'notifications'
type PermissionState = 'granted' | 'denied' | 'prompt'

request asks the user for the camera, the microphone, the location, speech recognition or notifications: it shows the OS dialog when they were never asked and resolves with their answer. Asked again, it resolves with the state as it stands, or the OS shows its dialog once more — that is the OS's call (below). Ask where the user expects it — the first tap on the record button, the first "share my location" — not at the app's start: a dialog nobody understands is a "denied" that stays.

TypeScript
const startCall = async () => {
  const camera = await permissions.request('camera')      // one dialog,
  const mic = await permissions.request('microphone')     // then the next
  if (camera === 'granted' && mic === 'granted') {
    console.log('call: starting')                         // open the call here
  } else {
    toast('A video call needs the camera and the microphone')
  }
}

UIScreen(UIButton(UIText('Start a video call')).onClick(startCall)).open()
  • camera and microphone are the device's. location is while the app is in use; there is no background location. speech is speech recognition's own permission: a dialog of its own on iOS, the microphone's on Android and the web. The speech plugin asks for speech and microphone both. notifications is the one switch behind local notifications (notifications.md) and remote push (push.md): iOS and Android 13+ show a dialog, Android before 13 has none (the state is the app's notification switch); on the desktop and the web it answers "granted" while the feature itself is absent.
  • The camera views and the plugins ask by themselves: Router.push(view) and view.open() of CameraView, QRScanner and ARScene ask before the view shows and reject with The camera permission was denied — a UICameraNode asks when its preview starts and sends that message to onError; AudioRecorder.start(), Geolocation and the speech plugin reject with e.message === 'denied'. A request before them only moves the dialog to a better moment.
  • request and query reject (an error, not a state) for a kind the host does not ask for: speech on an iOS build without the speech plugin.
Note

on iOS and Android the requests wait in a queue: two features asking at once get one dialog after the other, and one kind asked twice while its dialog is up gets one dialog and two answers.

Check whether a permission is granted: permissions.query

TypeScript
permissions.query(kind): Promise<PermissionState>   // the state now, never a dialog

query checks whether the camera, the microphone, the location, speech recognition or notifications are already granted, without a dialog: "granted", "denied" (refused, or blocked by the device's policy) or "prompt" (never asked: request will show the dialog). Use it to choose what a screen shows — the feature, a button that asks, or the way to the settings.

TypeScript
const nearby = UIColumn()

const showNearby = async () => {
  const state = await permissions.query('location')       // no dialog
  if (state === 'granted') {
    nearby.setContent([UIText('Places near you')])        // read the position here
  } else if (state === 'prompt') {                        // never asked: offer the button that asks
    nearby.setContent([UIButton(UIText('Use my location'))
      .onClick(() => permissions.request('location').then(showNearby))])
  } else {                                                // refused before
    nearby.setContent([UIText('Location is off for this app')])
  }
}

UIScreen(nearby).open()
showNearby()
Note

Android keeps no "never asked" state: query answers "prompt" until this app has asked once and "denied" after a refusal, even when the OS would show the dialog again. On the web the browser may not know before a request (Safari before 16, Firefox for the camera): it answers "prompt" then.

Explain why before the system dialog

iOS shows the dialog once, so show your own explanation first while the state is "prompt", and call request only when the user goes on. The dialog itself shows the usage text of the app's build (declared by the plugins).

TypeScript
const scan = () => console.log('scanner: open')            // open the scanner here

const why = UIScreen(
  UIText('The camera reads the receipt. Nothing leaves the phone.'),
  UIButton(UIText('Continue')).onClick(async () => {
    Router.pop()
    if ((await permissions.request('camera')) === 'granted') scan()   // the OS dialog now
  }),
)

const onScanTap = async () => {
  const state = await permissions.query('camera')
  if (state === 'prompt') Router.push(why)                 // never asked: explain first
  else if (state === 'granted') scan()
  else toast('Turn the camera on in Settings')
}

Router.init(UIScreen(UIButton(UIText('Scan a receipt')).onClick(onScanTap)))

Permission denied: send the user to Settings (openSettings)

TypeScript
permissions.openSettings(): void   // the app's own page of the system settings

When the user has denied the camera, the microphone or the location, the way back is the system settings: openSettings() opens the app's own page there (iOS: the app's page in Settings; Android: the app's info screen, with its Permissions). iOS never shows the dialog twice; Android stops asking when the OS decides. The user turns the permission on and comes back, and the app reads it again on "resume" (app.md).

TypeScript
const body = UIColumn()
const ready = () => body.setContent([UIText('Camera ready')])   // open the camera here

const askCamera = async () => {
  if ((await permissions.request('camera')) === 'granted') return ready()
  body.setContent([                                             // denied: the way back is Settings
    UIText('The camera is off for this app'),
    UIButton(UIText('Open Settings')).onClick(() => permissions.openSettings()),
  ])
}

// back from Settings: read the state again (query never shows a dialog)
app.addEventListener('resume', async () => {
  if ((await permissions.query('camera')) === 'granted') ready()
})

body.setContent([UIButton(UIText('Use the camera')).onClick(askCamera)])
UIScreen(body).open()
  • openSettings does nothing on the web, in the web player and on the desktop: on the web, tell the user to allow it in the browser's settings for the site.
  • Show a button that opens the settings; do not open them by surprise.
  • In the le.codes viewer app it opens the viewer's own page: the permissions there are the viewer's, the same for every project it runs.
Note

on Android the permission dialog pauses the app: "pause" and "resume" fire around it. Re-read with query in a "resume" handler, never request — that one can bring the dialog back after every answer.

What each platform answers: iOS, Android, the web

iOS Android the web, the web player
query the OS status "granted", else "prompt" until the app has asked once, "denied" after the browser's Permissions API; "prompt" where the browser cannot tell
request, never asked the system dialog the system dialog the browser's prompt: a camera or microphone stream stopped at once, a location fix (15 s limit)
request after a refusal "denied" at once, no dialog the OS decides: the dialog again, or "denied" the browser decides
openSettings the app's page in Settings the app's info screen nothing
  • The desktop and headless tests (lecodes render / lecodes test) have no dialog: query and request answer "granted" for every kind, and openSettings does nothing. Whether the feature exists there is the feature's own answer (CameraView.supported, Geolocation.supported, a rejection of the call).
  • On Android, location granted as "approximate" counts as "granted".
  • On the web a page served neither over https nor from localhost has no camera and no microphone: request answers "denied". speech is the microphone's permission there.

Declare the permissions of a published app: Info.plist and the Android manifest

A build of your own app (a lecodes app shell) declares a permission only through the plugins in app.json: the iOS usage text (NSCameraUsageDescription and the others, shown in the dialog) and the Android <uses-permission> come from their manifests. List the plugin that uses the permission, and it is declared:

kind declared by the plugins
camera camera (CameraView), qr-scanner, ar, webrtc
microphone audio-recorder, speech, webrtc
location geolocation
speech speech
JSON
{ "plugins": { "camera": "*" } }
  • A kind no listed plugin declares fails in the published app: iOS closes the app at a camera or microphone request (a usage text is required), shows no location dialog and rejects speech; Android answers "denied" with no dialog.
  • The le.codes viewer app declares every kind, so a project works there either way: check the plugins list before you publish.

A plugin's own permissions: @permission

A plugin's contract tags a method that needs a permission (@permission microphone in its contract.d.ts). The generated native code asks the host's same service before the call and rejects with denied when it was refused — the plugin's own code never shows a dialog — and lecodes plugin gen fails when the plugin's manifests lack the platform's usage text or <uses-permission>. See native plugins.

See also