最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there any alternative to navigator.permissions.query Permissions API? - Stack Overflow

programmeradmin1浏览0评论

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 : /

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

});

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

});
Share Improve this question edited Oct 16, 2018 at 2:16 Shiv Kumar Baghel asked Oct 12, 2018 at 17:34 Shiv Kumar BaghelShiv Kumar Baghel 2,4806 gold badges19 silver badges35 bronze badges 10
  • any particular name? – Jaromanda X Commented Oct 12, 2018 at 17:56
  • im checking for geolocation permission. – Shiv Kumar Baghel Commented Oct 12, 2018 at 18:01
  • On what platform(s)? – DaveAlden Commented Oct 13, 2018 at 16:17
  • Mobile Android. – Shiv Kumar Baghel Commented Oct 14, 2018 at 8:23
  • Have you tried android permission plugin? cordova.apache.org/docs/en/latest/guide/platforms/android/… – QuickFix Commented Oct 17, 2018 at 8:31
 |  Show 5 more comments

2 Answers 2

Reset to default 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.

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}
)
发布评论

评论列表(0)

  1. 暂无评论