18

Is there any alternative to navigator.permissions.query Permissions API query to check geolocation permission. cause its still in Working Draft and has less Browser compatibility.

W3C Permissions Ref : https://www.w3.org/TR/permissions/

Issue is app resume once user perform action on native permission popup then wanted to check the action being taken by user.

Hybrid Cordova App callback for location permission alert

Platform : Mobile Android

NOTE : Don't want to use cordova diagnostic plugin

Example:

navigator.permissions.query({name:'geolocation'}).then(function(result) {

  console.log('result : ', result);

});
Shiv Kumar Baghel
  • 2,464
  • 6
  • 18
  • 34

2 Answers2

9

You can directly use navigator.geolocation without asking permission first. it will automatically raise ask location prompt like the navigator.permissions do.

navigator.geolocation.getCurrentPosition(
  (i)=>console.log('success',i),
  (i)=>console.log('failed',i)
)

navigator.permissions is not supported in safari and edge, but navigator.geolocation is supported, so i think it's safe to just execute geolocation without checking permission because it also will raise prompt permission first.

kafinsalim
  • 435
  • 6
  • 15
  • 4
    Wonder if there's a way to know if the user has allowed access before. I don't just want to flat out ask them for it before explaining why I'm going to request it. In some cases, the user's instinct would be to just decline it. – Philll_t Sep 01 '20 at 01:05
  • navigator.permissions is now supported in edge https://caniuse.com/?search=navigator.permissions – berniecc Oct 01 '20 at 14:12
  • Any alternative for safari ? – ZecKa May 04 '22 at 10:41
3

I don't think so.

At this moment the navigator.permissions object is undefined - probably it's removed in WebView by purpose to not mix web permissions with android permissions.

Option 1:

You may try Cordova diagnostic plugin, specifically the getLocationAuthorizationStatus method which should return permission state in very similar way to Permissions API. Please note I haven't tried the plugin.

Option 2:

Trigger location permissions dialog by requesting location. When you'll receive PositionError with PERMISSION_DENIED constant code, it'll mean that user denied location permission (just now or at app settings).

navigator.getCurrentPosition(
  function(position) { /** won't be executed for such short timeout */ },
  function(positionError) {
    switch (positionError.code) {
    // PERMISSION_DENIED
    case 1:
      console.log('Permission denied')
      break
    // POSITION_UNAVAILABLE
    case 2:
      console.log('Permission allowed, location disabled')
      break
    // TIMEOUT
    case 3:
      console.log('Permission allowed, timeout reached')
      break
    }
  },
  {timeout: 0}
)
piotr_cz
  • 8,755
  • 2
  • 30
  • 25
  • thanks @piotr_cz for reply. but issue is i cant use the `diagnostic plugin` thats why i was looking for other solution. by your solution if im running `navigator.getCurrentPosition` then the permission native dialog is coming by which the app is going into `background` from `foreground` after `resume` to app i want to check that what user action performed on the dialog. – Shiv Kumar Baghel Oct 15 '18 at 02:29
  • You have to come with some workaround, because as I noted in [this answer](https://stackoverflow.com/questions/52662150/navigator-permissions-query-not-working-in-android-ios-webview/52807516#52807516) Permissions API is probably removed by design. – piotr_cz Oct 15 '18 at 11:38
  • 1
    according to **W3C** https://www.w3.org/TR/permissions/ its still in `Working Draft` so some Browsers has support some don't. – Shiv Kumar Baghel Oct 15 '18 at 11:47
  • Well, _Chrome Mobile_ 69.0.3497.100 has Permissions API, but _Android System WebView_ with the same version doesn't. – piotr_cz Oct 15 '18 at 12:26
  • yes true.i have hybrid app that will serve to different android `WebView`. so want a solution without using any `cordova` plugin like `Cordova diagnostic plugin` something. – Shiv Kumar Baghel Oct 16 '18 at 02:24
  • I'm not sure I understand. Are you using two WebViews in one app? – piotr_cz Oct 16 '18 at 11:06
  • no only one `webview` i have ... i only want to test if permission granted or not ....without calling `navigator.geolocation.getCurrentPosition`. leave the `webview` is it possible check in `JavaScript` if we **allow** or **denied** the **permission** – Shiv Kumar Baghel Oct 16 '18 at 11:09