<p class="stc" v-show="isIOS">如果摇动手机没有反应。请点击<a href="javascript:;" @click="showPopup" style="color:#0000FF" class="aClick">这里</a>授权</p>
function showPopup(){
if(window.localStorage.getItem('agree_IOS') == null){
// authorization.value = true;
consentToAuthorization()
}else {
document.getElementsByClassName('aClick').onclick = null;
}
}
判断本地是否有localstorage如果有就是点击过授权按钮并且点击了确定授权,如果没有就是没有授权或者根本没有点击过授权按钮。
function consentToAuthorization(){ //ios用户点击 '这里' 链接触发
window.localStorage.setItem('agree_IOS',true)
if (typeof(DeviceMotionEvent) !== 'undefined' && typeof(DeviceMotionEvent.requestPermission) === 'function') {
window.DeviceMotionEvent.requestPermission().then(permissionState => {
if (permissionState === "granted" && window.DeviceMotionEvent) { //允许
window.addEventListener('devicemotion',deviceMotionHandler, false);
}
if (permissionState === "denied") {
console.log("你拒绝了");
}
}).catch((err) => {
console.log(err,'err')
});
} else {
console.log('deviceMotionEvent is not defined')
}
}