webapp/html5 Location Services can't show user permission prompt again when click "Do not allow" option in Safari or UIWebview
在Safari或者UIWebview中当点击“不允许”选项之后webapp/html5 的定位服务不能再次显示用户权限提示框
以下是html5关键代码:
Objective-C
点击这个按钮,获得您的坐标:
试一下
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"
Longitude: " + position.coords.longitude;
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML="User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML="The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."
break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
点击这个按钮,获得您的坐标:
试一下
varx=document.getElementById("demo");
functiongetLocation()
{
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
functionshowPosition(position)
{
x.innerHTML="Latitude: "+position.coords.latitude+
"
Longitude: "+position.coords.longitude;
}
functionshowError(error)
{
switch(error.code)
{
caseerror.PERMISSION_DENIED:
x.innerHTML="User denied the request for Geolocation."
break;
caseerror.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
break;
caseerror.TIMEOUT:
x.innerHTML="The request to get user location timed out."
break;
caseerror.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."
break;
}
}
不管是原生iOS APP嵌入单个h5页面,还是都是h5页面的webapp也好,只要涉及到定位都会遇到这个情况。
当请求iOS开始定位的时候,会弹出一个提示框,提示你是否"允许"定位,允许定位继续下一步,如果一不小心点了下"不允许"之后,会发现,下次进到这个h5之后再也不会提示请求定位权限的提示框了。"不允许"被缓存了下来。再次调用geolocation会报 error.PERMISSION_DENIED 错误
而且没有相应的api能重启提示框。
解决办法很鸡肋,需要在系统中就行清理缓存的设置:
(设置)Settings -> (通用)Genera -> (重置) Reset-> (重置定位于隐私)Reset Location & Privacy.
(设置)Settings ->( 隐私)Privacy 开启 Location Services.
重新进入页面即可提示用户权限提示了!
如果说h5页面用于app中显示,可以尝试使用js与app原生交互来获取定位,原生获取定位后反传给h5,这样当整个app的定位权限被用户拒绝与开启的时候,直接到设置中对应的app下就可以恢复了。