前端移动端陀螺仪问题

因ios12.2版本以上后 safari浏览器设置了运动与方向的权限,前端怎么获取这个权限打开这个设置,或者是请求接口,

阅读 2.7k
1 个回答
<script>
  if (location.protocol != 'https:') {
   location.href = 'https:' + window.location.href.substring(window.location.protocol.length);
  }
 
function requestT () {
      if (typeof(DeviceMotionEvent) !== 'undefined' && typeof(DeviceMotionEvent.requestPermission) === 'function') {
          
          DeviceMotionEvent.requestPermission()
          .then(response => {
    alert('Orientation tracking '+ response);
          
            if (response == 'granted') {
              window.addEventListener('devicemotion', (e) => {
               document.getElementById('request').style.visibility='hidden';
              })
            }
          })
      .catch(console.error)
      }else {
          alert('DeviceMotionEvent is not defined');
      }

  }
  document.getElementById('request').onclick = requestT;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题