Is there any alternative to navigator.permissions.query Permissions API?
Asked Answered
P

2

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);

});
Potence answered 12/10, 2018 at 17:34 Comment(10)
any particular name?Afrit
im checking for geolocation permission.Potence
On what platform(s)?Wharve
Mobile Android.Potence
Have you tried android permission plugin? cordova.apache.org/docs/en/latest/guide/platforms/android/…Morice
@Morice don't want to use any cordova plugin here.Potence
@ShivKumarBaghel How about adding the permission to manifest xml using cordova custom config plugin so that you need not have to check for it? The plugin here is not to check permission but to set it.Northrop
sorry @Northrop i need to ask user's permission before getting his location.Potence
@ShivKumarBaghel If that's the case, you will not be able to check permissions without cordova plugins as it requires interaction with the device.Northrop
@Northrop navigator.permissions.query is available solution to check the permissions. but its still in working draft and has less browser support, specially in mobile browsers. so looking for alternatives.Potence
C
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.

Casals answered 13/5, 2020 at 3:44 Comment(3)
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.Controller
navigator.permissions is now supported in edge caniuse.com/?search=navigator.permissionsUlani
Any alternative for safari ?Peters
K
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}
)
Kellykellyann answered 14/10, 2018 at 17:28 Comment(7)
thanks @Kellykellyann 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.Potence
You have to come with some workaround, because as I noted in this answer Permissions API is probably removed by design.Kellykellyann
according to W3C w3.org/TR/permissions its still in Working Draft so some Browsers has support some don't.Potence
Well, Chrome Mobile 69.0.3497.100 has Permissions API, but Android System WebView with the same version doesn't.Kellykellyann
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.Potence
I'm not sure I understand. Are you using two WebViews in one app?Kellykellyann
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 permissionPotence

© 2022 - 2024 — McMap. All rights reserved.