Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

});
like image 925
Shiv Kumar Baghel Avatar asked Oct 12 '18 17:10

Shiv Kumar Baghel


People also ask

Can I use permissions API?

The Permissions API provides the tools to allow developers to implement a better user experience as far as permissions are concerned. For example, it can query whether permission to use a particular API is granted or denied, and specifically request permission to use an API.

What are API permissions?

The Permissions API allows a web application to be aware of the status of a given permission, to know whether it is granted, denied or if the user will be asked whether the permission should be granted.


2 Answers

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.

like image 169
kafinsalim Avatar answered Oct 21 '22 07:10

kafinsalim


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}
)
like image 3
piotr_cz Avatar answered Oct 21 '22 07:10

piotr_cz